牛客多校4 A Ternary String

Ternary String
时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
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 become11021” 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

题意:给一个由012组成的字符串,在每一秒钟字符‘2’的后面会产生一个1,字符1的后面会产生一个0,然后删除首位字符,问多少秒后字符串为空?
分析:当碰到字符‘0‘, t=t+1 t = t + 1 ,当碰到‘1’时,完全消除‘1’产生的字符 t=t2+2 t = t ∗ 2 + 2 ,当碰到‘2’时,完全消除‘2’ 产生的字符 t=32t+1 t = 3 ∗ 2 t + 1 ,由于字符串的长度最大为1e5.所以当出现太多次的2时,就需要采用扩展欧拉定理来做.
牛客多校4 A Ternary String_第1张图片

#include
#define LL long long
using namespace std;
const int N=1e5+6;
const LL MOD=1e9+7;
mapp;
LL Phi(LL n)
{
    if(n==1)return n;
    LL ret=n;
    for(LL i=2; i*i<=n; i++)
    {
        if(n%i==0)
        {
            ret-=ret/i;
            while(n%i==0)n/=i;
        }
    }
    if(n>1)ret-=ret/n;
    return ret;
}
LL pod(LL x,LL n,LL MOD)
{
    LL ret=1;
    for(; n; n>>=1,x=x*x%MOD)if(n&1)ret=ret*x%MOD;
    return ret;
}
bool check(LL n)
{
    LL ret=3;
    while(n--)
    {
        ret*=2;
        if(ret-3>=MOD)return true;
    }
    return false;
}
char s[N];
int mo[N];
int main()
{
    p[1]=1;
    LL tem=MOD;
    while(tem!=1){p[tem]=Phi(tem);tem=p[tem];}
    int TA,n;
    scanf("%d",&TA);
    while(TA--)
    {
        scanf(" %s",s+1);
        n=strlen(s+1);
        tem=MOD;
        for(int i=n; i>0; i--)
        {
            mo[i]=tem;
            if(s[i]=='2')tem=p[tem];
        }
        mo[0]=tem;
        LL ans=0;
        bool tag=false;
        for(int x=1; x<=n; x++)
        {
            LL mod=mo[x];
            if(tag)
            {
                if(s[x]=='0')(ans+=1)%=mod;
                if(s[x]=='1')ans=2*(ans+1)%mod;
                if(s[x]=='2')ans=3*pod(2,(ans+1)%p[mod]+p[mod],mod)%mod-3;
                ans%=mod;
                (ans+=mod)%=mod;
            }
            else
            {
                if(s[x]=='0')ans++;
                if(s[x]=='1')ans=2*(ans+1);
                if(s[x]=='2')
                {
                    ans=ans+1;
                    if(check(ans))
                    {
                        tag=true;
                        ans=((pod(2,ans,mod)*3-3)%mod+mod)%mod;
                    }
                    else
                    {
                        LL tem=1;
                        for(int i=1; i<=ans; i++)
                            tem*=2;
                        ans=tem*3-3;
                    }
                    //cout<

                }
                if(ans>=1e9+7)
                {
                    tag=1;
                    ans%=mod;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(【数学】)