NC214394. AbsoluteDifferenceEquation
描述
输入描述
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; }