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).
题意:
给你一个L(二进制),求有多少对<a,b> 满足
a+b≤L
a+b=a XOR b
很明显是dp的,然后我就不太会。好不容易弄了半天搞了个数位dp过去了。
听说是思维瞎搞??
首先可以由 a+b=a XOR b 得到
a | 0 | 0 | 1 | 1 |
b | 0 | 1 | 0 | 1 |
a+b(a xor b) | 0 | 1 | 1 | 0 |
也就是个异或关系
对于a,b两个变量不太好找dp转移关系,那么找(a+b)的转移关系
考虑拿数位dp搞一搞,因为这东西原版就是计算某一类特殊数的个数的
但这次我并不是想要计算特殊数,而是想要它的可以统计小于某一个数的所有数的性质
我们容易知道,对于任意一个(a+b),它的贡献就是2^(二进制中1的个数)
从数的高位向低位走。两个选择0或1,走到头的时候计算贡献(也就是走完全程,返回2^(二进制中1的个数))
记忆化搜索的时候,注意返回 记住的状态 乘以 2^(走到当前共有多少个1)
因为指数上 要统计的是二进制中1的个数,而此时通过记忆化减少向下递归,要返回了,那么就需要将当前的1的个数
和记住的状态乘一下。
limit是表示之前走来的时候是否状态已经到达最大值
/*author:revolIA*/
/*明日を変えるなら今日変えなきゃ*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7,mod = 1e9+7;
ll dp[maxn];
ll Pow2[maxn] = {1};
char str[maxn];
ll dfs(int len,int limit,int cnt){
//printf("%d %d %d\n",len,limit,cnt);
if(!len)return Pow2[cnt];
if(!limit && ~dp[len])return dp[len]*Pow2[cnt]%mod;
//printf("judge : %d\n",limit && (str[len] == '0'));
ll sum = dfs(len-1,limit && (str[len] == '0'),cnt);
if(!limit || str[len]-'0')sum += dfs(len-1,limit,cnt+1);
sum %= mod;
return (!limit)?dp[len] = sum : sum;
}
int main(){
memset(dp,-1,sizeof dp);
for(int i=1;i<maxn;i++)Pow2[i] = Pow2[i-1]*2%mod;
scanf("%s",str+1);
int n = strlen(str+1);
reverse(str+1,str+1+n);
printf("%lld\n",dfs(n,1,0));
return 0;
}