Problem Statement
12:17 (UTC): The sample input 1 and 2 were swapped. The error is now fixed. We are very sorry for your inconvenience.
There are NN children in AtCoder Kindergarten, conveniently numbered 11 through NN. Mr. Evi will distribute CC indistinguishable candies to the children.
If child ii is given aa candies, the child's happiness will become xaixia, where xixi is the child's excitement level. The activity level of the kindergarten is the productof the happiness of all the NN children.
For each possible way to distribute CC candies to the children by giving zero or more candies to each child, calculate the activity level of the kindergarten. Then, calculate the sum over all possible way to distribute CC candies. This sum can be seen as a function of the children's excitement levels x1,..,xNx1,..,xN, thus we call it f(x1,..,xN)f(x1,..,xN).
You are given integers Ai,Bi(1≦i≦N)Ai,Bi(1≦i≦N). Find modulo 109+7109+7.
Constraints
- 1≦N≦4001≦N≦400
- 1≦C≦4001≦C≦400
- 1≦Ai≦Bi≦400(1≦i≦N)1≦Ai≦Bi≦400(1≦i≦N)
Partial Score
- 400400 points will be awarded for passing the test set satisfying Ai=Bi(1≦i≦N)Ai=Bi(1≦i≦N).
Input
The input is given from Standard Input in the following format:
NN CC A1A1 A2A2 ... ANAN B1B1 B2B2 ... BNBN
Output
Print the value of modulo 109+7109+7.
Sample Input 1 Copy
Copy
2 3 1 1 1 1
Sample Output 1 Copy
Copy
4
This case is included in the test set for the partial score, since Ai=BiAi=Bi. We only have to consider the sum of the activity level of the kindergarten where the excitement level of both child 11 and child 22 are 11 (f(1,1)f(1,1)).
- If child 11 is given 00 candy, and child 22 is given 33 candies, the activity level of the kindergarten is 10∗13=110∗13=1.
- If child 11 is given 11 candy, and child 22 is given 22 candies, the activity level of the kindergarten is 11∗12=111∗12=1.
- If child 11 is given 22 candies, and child 22 is given 11 candy, the activity level of the kindergarten is 12∗11=112∗11=1.
- If child 11 is given 33 candies, and child 22 is given 00 candy, the activity level of the kindergarten is 13∗10=113∗10=1.
Thus, f(1,1)=1+1+1+1=4f(1,1)=1+1+1+1=4, and the sum over all ff is also 44.
思路:dp[i][j]表示前i个人分j块糖果的所有方案数的价值总和。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll p[406][406];//p[i][j]=i的j次方。
ll pre[406][406];//pre[i][j]:1^j+2^j+......i^j;
ll dp[406][406]={};
ll a[406],b[406];
int main()
{
p[0][0]=1;
for(ll i=1;i<=400;i++)
{
p[i][0]=1;
pre[i][0]=1;
for(ll j=1;j<=400;j++)
{
p[i][j]=(p[i][j-1]*i)%mod;
}
}
for(ll i=1;i<=400;i++)
for(ll j=0;j<=400;j++)
{
pre[i][j]=(pre[i-1][j]+p[i][j])%mod;
}
ll n,c;
cin>>n>>c;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
for(ll i=1;i<=n;i++)
{
cin>>b[i];
}
dp[0][0]=1;
for(ll i=1;i<=n;i++)
{
for(ll j=0;j<=c;j++)
{
for(ll k=0;k<=j;k++)
{
ll y=(pre[b[i]][k]-pre[a[i]-1][k]+mod)%mod;
dp[i][j]=(dp[i][j]+dp[i-1][j-k]*y)%mod;
dp[i][j]%mod;
}
}
}
cout<<dp[n][c]%mod<<endl;
}