You are given a positive integer L in base two. How many pairs of non-negative integers (a,b) satisfy the following conditions?
·a+b≤L
·a+b=a XOR b
Since there can be extremely many such pairs, print the count modulo 109+7.
→What is XOR?
The XOR of integers A and B, A XOR B, is defined as follows:
·When A XOR B is written in base two, the digit in the 2k's place (k≥0) is 1 if either A or B, but not both, has 1 in the 2k's place, and 0 otherwise.
For example, 3 XOR 5=6. (In base two: 011 XOR 101=110.)
Constraints
·L is given in base two, without leading zeros.
·1≤L<2100001
输入
Input is given from Standard Input in the following format:
L
输出
Print the number of pairs (a,b) that satisfy the conditions, modulo 109+7.
样例输入 Copy
【样例1】 10 【样例2】 1111111111111111111
样例输出 Copy
【样例1】 5 【样例2】 162261460
提示
样例1解释:Five pairs (a,b) satisfy the conditions: (0,0),(0,1),(1,0),(0,2) and (2,0).
考虑到(a+b=a^b),根据异或的性质,两个数的选择为:10或01或00. 当高位确定为1时,可以a这一位为1,可以b这一位为1,当高位为0时,无限制,dp【i】【1】代表这一位确定,dp【i】【0】代表这一位不确定,不要忘记这一位的可以继承前一位是0的个数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxx=1e6+7;
ll dp[maxx][3];
char a[maxx];
int main()
{
ll i,j,k,n,m,x,y;
scanf("%s",a+1);
n=strlen(a+1);
dp[0][1]=1;
for(i=1;i<=n;i++)
{
dp[i][0]=(dp[i-1][0]*3)%mod;
if(a[i]=='1')
{
dp[i][1]=(dp[i-1][1]*2)%mod;
dp[i][0]=(dp[i][0]+dp[i-1][1])%mod;
}
else dp[i][1]=dp[i-1][1];
}
printf("%lld\n",(dp[n][1]+dp[n][0])%mod);
return 0;
}