We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<algorithm>#include<cstdio>usingnamespacestd;typedeflonglongll;#define FOR(i, a, b) for (int i = (a); i < (b); i++)#define REP(i, n) for (int i = 0; i < (n); i++)#define ROF(i, a, b) for (int i = (b); --i >= (a); )intri(){intx;scanf("%d",&x);returnx;}constintN=1001,MOD=1000000007;chara[N];intbinom[N][N],dp[N];intpr[168],mu[N],np=0;boolsieved[N];voidgetPrimes(){mu[1]=1;for(inti=2;i<N;i++){if(!sieved[i]){pr[np++]=i;mu[i]=-1;}for(intj=0;j<np&&i*pr[j]<N;j++){sieved[i*pr[j]]=true;if(i%pr[j]==0){mu[i*pr[j]]=0;break;}mu[i*pr[j]]=-mu[i];}}}intmain(){getPrimes();REP(i,N+1){binom[i][0]=binom[i][i]=1;FOR(j,1,i)binom[i][j]=(binom[i-1][j-1]+binom[i-1][j])%MOD;}for(intcc=ri();cc--;){intn=ri(),k=ri(),ans=0;scanf("%s",a);FOR(i,2,n+1)if(n%i==0&&mu[i]){intm=n/i;fill_n(dp,k+1,0);dp[0]=1;REP(j,m){intx=0;for(intg=j;g<n;g+=m)x+=a[g]=='1';inty=i-x;ROF(g,0,k+1)dp[g]=((g<x?0:dp[g-x])+(g<y?0:dp[g-y]))%MOD;}llsum=0;REP(j,k+1)sum=(sum+dp[j])%MOD;ans=(ans+sum*mu[i])%MOD;}REP(i,k+1)ans=(ans+binom[n][i])%MOD;printf("%d\n",(ans+MOD)%MOD);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
String Transmission
You are viewing a single comment's thread. Return to all comments →
C++ Solution