牛客网第4场A

时间:2021-08-16 16:28:59
链接:https://www.nowcoder.com/acm/contest/142/A
来源:牛客网 题目描述
A ternary string is a sequence of digits, where each digit is either , , or .
Chiaki has a ternary string s which can self-reproduce. Every second, a digit is inserted after every in the string, and then a digit is inserted after every in the string, and finally the first character will disappear.
For example, ``'' will become ``'' after one second, and become ``'' 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 ( + ).
输入描述:
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 ( ≤ |s| ≤ ).
It is guaranteed that the sum of all |s| does not exceed x .
输出描述:
For each test case, output an integer denoting the answer. If the string never becomes empty, output - instead.
示例1
输入 复制 输出 复制

欧拉降幂且记录;

(1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒。

(2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒。

(3)如果在消除一个 2 前经过了 n 秒,那么消掉这个 2 与其产生的所有数需要 (2 ^ (n + 1) - 1) * 3 秒。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<map>
#include<string.h>
using namespace std;
#define ll long long
map<ll,ll>mp;
char s[];
ll phi(ll x)
{
if(mp[x]) return mp[x];
ll temp=x;
ll ans=x;
if(x==) return mp[]=;
for(ll i=;i*i<=x;i++){
if(x%i==){
ans=ans/i*(i-);
while(x%i==) x/=i;
}
}
if(x>) ans=ans/x*(x-);
return mp[temp]=ans;
} ll qsm(ll a,ll b,ll c)
{
ll ret = ;
for (;b;b >>= ,(a *= a)%=c)
if (b & ) (ret *= a)%=c;
return ret;
}
ll solve(ll x,ll mod)
{
if(x==||mod==)
return ;
if(s[x]==''){
return (1LL+solve(x-,mod)+mod)%mod;
}else if(s[x]==''){
return (*(solve(x-,mod)+)+mod)%mod;
}else{
ll ph=phi(mod);
ll t=(solve(x-,ph))%ph;
return (qsm(,t,mod)*%mod-+mod*)%mod;
} }
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s+);
printf("%lld\n",solve(strlen(s+),1e9+));
}
return ;
}

牛客网第4场A