Sum Equals Xor (DP)

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; 
}

定。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值