Bits Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 764 Accepted Submission(s): 203
Problem Description
If the quantity of '1' in a number's binary digits is n, we call this number a n-onebit number. For instance, 8(1000) is a 1-onebit number, and 5(101) is a 2-onebit number. Now give you a number - n, please figure out the sum of n-onebit number belong to [0, R).
Input
Multiple test cases(less than 65). For each test case, there will only 1 line contains a non-negative integer n and a positive integer
R(n≤1000,0<R<21000)
, R is represented by binary digits, the data guarantee that there is no leading zeros.
Output
For each test case, print the answer module 1000000007 in one line.
Sample Input
1 1000
Sample Output
7
题意: 给一个数n和一个二进制串,问你小于这个二进制串的只有n个1的二进制串有多少个
思路: 考虑每一个1,我们先假设不取这个1,那么在剩下的m个位置里随意选n个即可,组合数为C(m,n),观察可知每一个位上的1的数量是相同的,为C(m-1,n-1)
那么只要每次用前缀和*个数+(2º+2¹+...2的(m-1)次方)*C(m-1,n-1)即可
但是有个问题,R明明小于2的1000次方,如果我n开小于2000就WA? 没理解为什么....
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long mod=1000000007;
#define N 10100
char r[N];
long long d[N];
long long s[N],sum[N];
long long c[N][N];
long long pow_mod(long long a,long long n)
{
long long ans=1;
while(n)
{
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
void init()
{
c[0][0]=1;
for(int i=1;i<=1000;i++)
{
for(int j=0;j<=i;j++)
{
if(j==0||j==i) c[i][j]=1;
else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
for(long long i=0;i<=1000;i++)
{
s[i]=pow_mod(2,i);
if(!i) sum[i]=s[i];
else sum[i]=(sum[i-1]+s[i])%mod;
}
}
int main()
{
long long n;
init();
while(~scanf("%lld",&n))
{
scanf("%s",r);
long long len=strlen(r);
long long cnt=0;
for(long long i=0; i<len; i++)
if(r[i]=='1')
d[cnt++]=i;
long long t=0;
long long ans=0,num=0;
while(1)
{
long long dist=len-d[t]-1;
if(dist>=n)
ans=(ans+(num*c[dist][n]%mod+sum[dist-1]*c[dist-1][n-1]%mod)%mod)%mod;
num=(num+s[len-1-d[t]])%mod;t++;n--;
if(n==0&&t!=cnt)
{
ans=(ans+num)%mod;
break;
}
if(t==cnt) break;
}
printf("%lld\n",(ans+mod)%mod);
}
return 0;
}