题意:给定a和b,n,让你求b+a, b+2*a, .......b+n*a里面有多少1.
当统计第K位的时候 可以注意到 第 B+T*A 和 B+(T+2^(K+1))*A 位是相同的
那么 第K位的时候 只需要统计2^(K + 1) - 1次就可以了
当统计第K位的时候 可以注意到 连续的 (2^K)/A都是连续的0 或者连续的1 所以可以考虑直接连续记录(2^K)/A个结果。
那么 第K位的时候 只需要统计N / ((2^K)/A)次就可以了
那么 第K位的时候 只需要统计 2^K/((2^K)/A) 复杂度 变为O(A)
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
void Solve(LL a,LL b,LL n)
{
LL cnt=0;
LL max=b+a*n;
for(LL i=0;i<64;i++)
{
LL m=(LL)1<<i;
LL mm=m;
if(m>max) break;
m<<=1;
LL cur=a+b;
LL j=0;
while(j<m&&j<n)
{
LL upper=cur+(mm-(cur%mm))-(LL)1;
LL step=(upper-cur)/a+(LL)1;
if(j+step>=n) step=n-j;
if(j+step>=m) step=m-j;
if(cur&(LL)1<<i)
{
cnt+=step*(n/m);
if(j+step<(n%m)) cnt+=step;
else if(j<(n%m)) cnt+=(n%m)-j;
}
cur+=a*step;
j+=step;
}
}
cout<<cnt<<endl;
}
int main()
{
int t,k=1;
LL a,b,n,i,j;
cin>>t;
while(t--)
{
cin>>a>>b>>n;
cout<<"Case #"<<k++<<": ";
Solve(a,b,n);
}
return 0;
}