5185 Equation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Puppettt/article/details/77010450

Equation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 656    Accepted Submission(s): 230


Problem Description
Gorwin is very interested in equations. Nowadays she gets an equation like this
x1+x2+x3++xn=n, and here
0xinfor1inxixi+1xi+1for1in1

For a certain  n, Gorwin wants to know how many combinations of  xi satisfies above condition.
For the answer may be very large, you are expected output the result after it modular  m.
 

Input
Multi test cases. The first line of the file is an integer  T indicates the number of test cases.
In the next  T lines, every line contain two integer  n,m.

[Technical Specification]
1T<20
1n50000
1m1000000000
 

Output
For each case output should occupies one line, the output format is Case #id: ans, here id is the data number starting from 1, ans is the result you are expected to output.
See the samples for more details.
 

Sample Input
 
  
2 3 100 5 100
 

Sample Output
 
  
Case #1: 2 Case #2: 3
 

Source


题意:n个数满足  0<=X(i)<=n   X(i+1) 等于 X(i)或X(i)+1, 因此第一个X1 必定小于等于1 ( 若X1=2  则n个数最小为2*n ) 
          最理想的状态为1+2+3+...+x=n;  则可以求出xi最大值  [(1+x)*x]/2 =n;   
          其中x1~xn去重以后 一定是连续的 可以由dp[i][j]表示 由1~i种数字构成的和为j的组合数
          状态转移方程为dp[i][j]=dp[i][j-i]+dp[i-1][j-i]   ( dp[i][j-i]表示在n=j-i的组合数字的基础上在xn=i 后面增加一个数字i  ;dp[i-1][j-1] 在n=j-i的组合数字的基础上在xn=i-1 后面增加一个 数字i)

AC:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
int dp[500][50050]={0};
int main()
{
    int t;scanf("%d",&t);
    int v=1;
    while(t--)
    {
        LL n,mod;
        scanf("%lld%lld",&n,&mod);
        int k=1;
        while(k*(k+1)<=2*n)k++;
        k--;

        for(int i=0;i<=n;i++)dp[0][i]=0;
        for(int i=0;i<=k;i++)dp[i][0]=0;

        dp[0][0]=1;
        for(int i=1;i<=k;i++)
            for(int j=i;j<=n;j++)
            dp[i][j]=(dp[i][j-i]+dp[i-1][j-i])%mod;

        LL ans=0;
        for(int i=1;i<=k;i++)
            ans+=dp[i][n],ans%=mod;
        printf("Case #%d: %lld\n",v++,ans);

    }
}


           

猜你喜欢

转载自blog.csdn.net/Puppettt/article/details/77010450
今日推荐