E - キャンディーとN人の子供 / Children and Candies(dp+前缀和优化)

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值