Absolute Difference Equation(2020年湖南acm第四题)

这篇博客主要探讨了一个关于字符串序列的操作问题,其中涉及将包含0、1和问号的序列通过特定运算转化为单一元素1的方案数。代码实现中,利用了位操作和快速幂算法计算不同替换方式下的结果。博客内容适合对算法和编程感兴趣的读者,尤其是关注数据结构和算法的初学者。
摘要由CSDN通过智能技术生成

Description

For a sequence a_1, a_2, \ldots, a_na1​,a2​,…,an​, consider the following operation ff: f(a)={b_1, b_2, \ldots, b{n-1}}f(a)=b1​,b2​,…,bn−1, where b_i=|a_i-a{i+1}|bi​=∣ai​−ai+1∣.

After applying the operation ff for n-1n−1 times, denoted by f^{n-1}(a)fn−1(a), the sequence will become a single element.

Bobo has a sequence aa of length nn filled with0,1and?. He would like to know the number of ways modulo (10^9+7)(109+7) to replace?to0or1such that f^{n-1}(a)={1}fn−1(a)=1.

Input

The input consists of several test cases terminated by end-of-file. For each test case:

The first line contains a nonempty string aa consisting only of0,1and?, denoting the given sequence.

  • 1 \leq |a| \leq 10^61≤∣a∣≤106

  • The sum of |a|∣a∣ does not exceed 10^7107.

Output

For each test case, print an integer which denotes the result.

sample input1:1

sample output1:1

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
#define ll long long
ll pow_(ll a,ll b){
	ll an=1;
	while(b){
		if(b&1)
		an=an*a%mod;
		b/=2;
		a=a*a%mod;
	}
	return an;
}
char s[1000005];
int main(){
	while(~scanf("%s",s+1)){
		int ls=strlen(s+1);
		int c1=0,c2=0,c3=0,c4=0;
		for(int i=1;i<=ls;++i)
		if(s[i]=='?'&&((ls-1)&(i-1))==i-1)++c1;
		    else if(s[i]=='?')++c2;
		    else if(s[i]=='1'&&((ls-1)&(i-1))==i-1)++c3;
	  	c3%=2;
	  	if(c3==0&&c1==0)
	      	 printf("0\n");
	  	else if(c1==0)
	  	     printf("%lld\n",pow_(2,c2));
	  	else printf("%lld\n",pow_(2,c1+c2-1));
	}
}

多多支持博主,谢谢各位!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值