C - And and Pair (公式推导)

原题链接

Given an extremely large non-negative integer nn, you are asked to count the number of pairs (i,j)(i,j) of integers satisfying the following conditions:

  • 0\leq j\leq i\leq n0≤j≤i≤n;
  • ii & n=in=i; and
  • ii & j=0j=0.

Here & represents the bitwise AND operator.

For simplicity, the binary representation of nn will be given. Meanwhile, you only need to output the answer modulo (10^9+7)(109+7).

Input

The first line contains an integer T~(1\le T\le 20)T (1≤T≤20) indicating the number of test cases.

Each of the following TT lines contains a string S~(1 \le |S| \le 10^5)S (1≤∣S∣≤105) which is the binary representation of the non-negative integer nn. Note that leading zeros of SS could appear in the input.

Output

For each test case, output a line containing the answer described above modulo (10^9+7)(109+7).

Sample 1

InputcopyOutputcopy
2
111
1010
14
15

思路:

1,一般从右往左,是二进制位从低位到高位,这题用了前缀和,以及公式推导

2,以101110为例,当在1110时,2^1*(2^2*c(2,0)+2^1*c(2,1)+2^0*c(2,2))=2^1*(9)=2^1*3^num(num是前缀一的个数)

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=1e5+100,mod=1e9+7,inf=0x7f7f7f7f7f7f7f7f;
int ksm(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
void solve()
{
    string s;cin>>s;
    int num=0,tot=0,ans=0;
    for(int i=s.size()-1;i>=0;--i){
        tot++;
        if(s[i]=='1'){
            ans=(ans+ksm(2,tot-1-num)*ksm(3,num)%mod)%mod;
            num++;
        } 
    }cout<<ans+1<<'\n';
}
int32_t main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值