链接:https://www.nowcoder.com/acm/contest/142/A
来源:牛客网
A ternary string is a sequence of digits, where each digit is either 0, 1, or 2.
Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every 2 in the string, and finally the first character will disappear.
For example, ``212'' will become ``11021'' after one second, and become ``01002110'' after another second.
Chiaki would like to know the number of seconds needed until the string become an empty string. As the answer could be very large, she only needs the answer modulo (109 + 7).
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains a ternary string s (1 ≤ |s| ≤ 105).
It is guaranteed that the sum of all |s| does not exceed 2 x 106.
For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.
示例1
复制
3
000
012
22
复制
3
93
45
#include
#define rep(i,a,b) for(int i=a;i1)ans=ans/x*(x-1);
return ans;
}
int cnt=1;
void init(){
phi[0]=mod;
while(phi[cnt-1]!=1){
phi[cnt]=Phi(phi[cnt-1]);
cnt++;
}
}
ll pow_mod(ll base, ll x,ll mod){
//
if((mod&(mod-1))==0&&x>30)return 0;
ll ans=1;
while(x){
if(x&1) ans=ans*base%mod;
base=base*base%mod;
x>>=1;
}
return ans%mod;
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T--){
std::vector res(29);//这样的话,就不用清空了
scanf(" %s", str);
int len=strlen(str),now=0;
rep(i,0,len){
if(str[i]=='0'){
now=std::min(now+1,phi[0]);
rep(j,0,28){
++res[j];
res[j]%=phi[j];
}
}
else if(str[i]=='1'){
now=std::min(now*2+2,phi[0]);
rep(j,0,28){
res[j]=2*res[j]+2;
res[j]%=phi[j];
}
}
else{
// x -> 3 * (2 ^ (x + 1) - 1)
rep(j,0,28){
int e=(res[j+1]+1)%phi[j+1];
if(now+1>=phi[j+1])e+=phi[j+1];
res[j]=3*(pow_mod(2,e,phi[j])-1)%phi[j];
if(res[j]<0)res[j]+=phi[j];
}
if(now<=30) now=std::min(phi[0],(3ll<<(now+1))-3);
else now=phi[0];
}
}
//printf("ss:%lld s:%lld flag:%d\n",ss,s,flag)
printf("%d\n", res[0]);
}
return 0;
}
#include
#define Min(x,y) ((x<=y)?x:y)
#define rep(i,a,b) for(int i=a;i1)ans=ans/x*(x-1);
return ans;
}
int cnt=1;
void init(){
phi[0]=mod;
while(phi[cnt-1]!=1){
phi[cnt]=Phi(phi[cnt-1]);
cnt++;
}
}
ll _pow(ll bas, ll x,ll mod){
//
if(mod&(mod-1)==0&&x>30)return 0;
ll ans=1;
while(x){
if(x&1) ans=(ll)ans*bas%mod;
bas=(ll)bas*bas%mod;
x>>=1;
}
return ans%mod;//!!!记住要加上
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T--){
scanf(" %s", str);
int len=strlen(str),num=0;
rep(i,0,len) if(str[i]=='2') num++;
int s=0;
int now=0, mo;
rep(i,0,len){
if(str[i]=='0'){
now=std::min(now+1,phi[0]);//!!!
if(num>28)mo=1;else mo=phi[num];
s=(s+1)%mo;
}
else if(str[i]=='1'){
now=std::min(now*2+2,phi[0]);//!!!
if(num>28)mo=1;else mo=phi[num];
s=(s*2+2)%mo;
}
else{
// x -> 3 * (2 ^ (x + 1) - 1)
int mo1,mo2;
if(num>28)mo1=1,mo2=1;
else mo1=phi[num],mo2=phi[num-1];
int e;
if(now+1(phi[0],(3ll<<(now+1))-3);//!!!真实值,不得不说,移位是真的骚;还有这个min(),这样就可以都是int的类型,还不溢出
else now=phi[0];
//printf("now:%d s:%d num:%d\n",now,s,num);
num--;
}
}
//printf("ss:%lld s:%lld flag:%d\n",ss,s,flag);
printf("%d\n",s);
}
return 0;
}