列表

详情


NC214394. AbsoluteDifferenceEquation

描述

For a sequence a1, a2, ... , an, consider the following operation f: f(a) = {b1, b2, ... , bn-1}, where bi = |ai - ai+1|.
After applying the operation f for n - 1 times, denoted by fn-1(a), the sequence will become a single element.
Bobo has a sequence a of length n filled with 0, 1 and ?. He would like to know the number of ways modulo (109+7) to replace ? to 0 or 1 such that fn-1(a) = {1}. 

输入描述

The input consists of several test cases terminated by end-of-file. For each test case:
The first line contains a nonempty string a consisting only 0, 1, and ?, denoting the giving sequence.
 · 1 ≤ |a| ≤ 106
 · The sum of |a| does not exceed 107

输出描述

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

示例1

输入:

1
?????
1010?1?0

输出:

1
16
2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 43ms, 内存消耗: 1836K, 提交时间: 2021-10-24 13:37:43

#include <bits/stdc++.h>
using namespace std;
const int N=1000010, M=1e9+7;
typedef long long LL;
int n;
char s[N];
int qmi(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)res=(LL)res*a%M;
		a=(LL)a*a%M;
		b>>=1;
	}
	return res%M;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(cin>>(s+1))
	{
		n=strlen(s+1);
		int js=0,os=0,t=0;
		for(int i=1;i<=n;i++)
		{
			if((n-1&i-1)==i-1)
			{
				if(s[i]=='?')js++;
				else t^=(s[i]-'0');
			}	
			else if(s[i]=='?')os++;
		}
		int ans=0;
		if(!js)
		{
			if(t==1)ans=qmi(2,os);
		}
		else
		{
			ans=qmi(2,js+os-1);
		}
		cout<<ans<<"\n";
	}
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 458ms, 内存消耗: 11452K, 提交时间: 2023-04-27 16:37:56

#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)

using namespace std;
typedef long long LL;
const int N=1000005,mod=1000000007;
int n;
char s[N];

int main()
{
	while(cin>>(s+1))
	{
		n=strlen(s+1);
		LL pw=1,cnt=0,bit=0;
		rep(i,1,n)
			if(((n-1)&(i-1))==(i-1))
			{
				if(s[i]=='?') ++cnt;
				else if(s[i]=='1') bit^=1;
			}
			else if(s[i]=='?') pw=pw*2%mod;
		if(!cnt)
		{
			if(bit) printf("%lld\n",pw);
			else puts("0");
		}
		else
		{
			rep(i,1,cnt-1) pw=pw*2%mod;
			printf("%lld\n",pw);
		}
	}
	return 0;
}

上一题