hdu 3398

时间:2023-12-12 11:21:32

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
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
const long long mod = ; bool s[];
int prime[],len;
void Init(){
int i,j;
memset(s,false,sizeof(s));
len=;
for(i=;i<=;i++)
{
if(s[i]==true) continue;
prime[++len]=i;
if(i>) return;
for(j=i*;j<=;j=j+i)
s[j]=true;
}
}
int get_num(int n,int m){
int ans=;
while(n){
n=n/m;
ans=ans+n;
}
return ans;
}
LL pow_mod(LL a,LL b)
{
LL ans=;
while(b)
{
if(b&){
ans=(ans*a)%mod;
}
b=b>>;
a=(a*a)%mod;
}
return ans;
} void solve(int n,int m){
int ans;
LL sum1=,sum2=;
for(int i=;i<=len;i++)
{
if(prime[i]>n+m)break;
ans=get_num(n+m,prime[i])-get_num(n,prime[i])-get_num(m,prime[i]);
sum1=(sum1*pow_mod(prime[i],ans))%mod; ans=get_num(n+m,prime[i])-get_num(n+,prime[i])-get_num(m-,prime[i]);
sum2=(sum2*pow_mod(prime[i],ans))%mod;
}
// printf("%lld %lld\n",sum1,sum2);
if(sum1<sum2) sum1=sum1-sum2+mod;
else sum1=sum1-sum2;
printf("%lld\n",sum1);
}
int main()
{
Init();
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
solve(n,m);
}
return ;
}