题解:洛谷P6078 [CEOI2004] Sweets

这是一道紫题!

我们需要接触一个叫做生成函数的登西。

洛谷


思路

对于至少 $a$ 个,不超过 $b$ 个的限制,可以先求出限制不超过 $b$ 个的方案数,然后减去限制不超过 $a-1$ 个的方案数,即为答案。

对第 $i$ 个糖果罐列出其的生成函数,得:

因此,设答案为G(x):

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>

using namespace std;

#define ll long long

const ll mod=2004;

ll n,a,b;
ll c[15];

ll C(ll x,ll y)
{
if(y>x)
{
return 0;
}

ll ans=1,ans1=1;

for(ll i=1;i<=y;i++)
{
ans1*=i;
}
for(ll i=x;i>x-y;i--)
{
ans=(ans*i)%(mod*ans1);
}

return ans/ans1;
}

ll dfs(ll x,ll y,ll sum)
{
if(x>n)
{
return y*C(sum+n,n)%mod;
}

return (dfs(x+1,y,sum)+dfs(x+1,-y,sum-c[x]-1))%mod;
}

int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);

cin>>n>>a>>b;

for(ll i=1;i<=n;i++)
{
cin>>c[i];
}

cout<<((dfs(1,1,b)-dfs(1,1,a-1))%mod+mod)%mod<<"\n";

return 0;
}

咕了,学不懂数学

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×