hdu 3398

String

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2161    Accepted Submission(s): 628


Problem Description
Recently, lxhgww received a task : to generate strings contain '0's and '1's only, in which '0' appears exactly m times, '1' appears exactly n times. Also, any prefix string of it must satisfy the situation that the number of 1's can not be smaller than the number of 0's . But he can't calculate the number of satisfied strings. Can you help him?
 

 

Input
T(T<=100) in the first line is the case number.
Each case contains two numbers n and m( 1 <= m <= n <= 1000000 ).
 

 

Output
Output the number of satisfied strings % 20100501.
 

 

Sample Input
1
2 2
 

 

Sample Output
2
 
Author
lxhgww
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3400 3402 3401 3399 3404
 
转化成Cn+m n - Cn+m n+1
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef long long LL;
 7 const long long mod = 20100501;
 8 
 9 bool s[2000010];
10 int prime[150000],len;
11 void Init(){
12     int i,j;
13     memset(s,false,sizeof(s));
14     len=0;
15     for(i=2;i<=2000009;i++)
16     {
17         if(s[i]==true) continue;
18         prime[++len]=i;
19         if(i>2000001) return;
20         for(j=i*2;j<=2000009;j=j+i)
21         s[j]=true;
22     }
23 }
24 int get_num(int n,int m){
25     int ans=0;
26     while(n){
27         n=n/m;
28         ans=ans+n;
29     }
30     return ans;
31 }
32 LL pow_mod(LL a,LL b)
33 {
34     LL ans=1;
35     while(b)
36     {
37         if(b&1){
38             ans=(ans*a)%mod;
39         }
40         b=b>>1;
41         a=(a*a)%mod;
42     }
43     return ans;
44 }
45 
46 void solve(int n,int m){
47     int ans;
48     LL sum1=1,sum2=1;
49     for(int i=1;i<=len;i++)
50     {
51         if(prime[i]>n+m)break;
52         ans=get_num(n+m,prime[i])-get_num(n,prime[i])-get_num(m,prime[i]);
53         sum1=(sum1*pow_mod(prime[i],ans))%mod;
54 
55         ans=get_num(n+m,prime[i])-get_num(n+1,prime[i])-get_num(m-1,prime[i]);
56         sum2=(sum2*pow_mod(prime[i],ans))%mod;
57     }
58    // printf("%lld %lld\n",sum1,sum2);
59     if(sum1<sum2) sum1=sum1-sum2+mod;
60     else sum1=sum1-sum2;
61     printf("%lld\n",sum1);
62 }
63 int main()
64 {
65     Init();
66     int T,n,m;
67     scanf("%d",&T);
68     while(T--){
69         scanf("%d%d",&n,&m);
70         solve(n,m);
71     }
72     return 0;
73 }
View Code

 

posted @ 2014-04-26 12:08  芷水  阅读(181)  评论(0编辑  收藏  举报