QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577275#6891. String and GCDJZYZ#AC ✓2596ms210964kbC++141.9kb2024-09-20 09:58:522024-09-20 09:58:53

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 09:58:53]
  • 评测
  • 测评结果:AC
  • 用时:2596ms
  • 内存:210964kb
  • [2024-09-20 09:58:52]
  • 提交

answer


#include<bits/stdc++.h>
using namespace std;
template <typename T>inline void read(T &x)
{
    x=0;char c=getchar();bool f=0;
    for(;c<'0'||c>'9';c=getchar())f|=(c=='-');
    for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c-'0');
    x=(f?-x:x);
}
const int N = 1e6+7;
char s[N];
int bd[N],n;
vector<int> vec[N],factor[N];
int phi[N],v[N],prime[N],tot=0;
void init(int n)
{
    phi[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!v[i])
        {
            v[i]=i;
            prime[++tot]=i;
            phi[i]=i-1;
        }
        for(int j=1;j<=tot;j++)
        {
            if(prime[j]>v[i]||1ll*i*prime[j]>n)break;
            v[i*prime[j]]=prime[j];
            if(i%prime[j]==0)
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            phi[i*prime[j]]=phi[i]*(prime[j]-1);
        }
    }
    for(int i=1;i<=n;i++)
    for(int j=i;j<=n;j+=i)
    factor[j].push_back(i);
}
int buc[N],ans=1;
const int mod = 998244353;
void dfs(int x)
{
    if(x)
    {
        long long ret=0;
        for(int j:factor[x])
        {
            ret+=1ll*buc[j]*phi[j];
            buc[j]++;
        }
        ans=1ll*ans*(ret+1)%mod;
    }
    for(int y:vec[x])dfs(y);
    if(x)
    {
        for(int j:factor[x])
        {
            buc[j]--;
        }
    }
}
int main()
{
    //freopen("Sum.in","r",stdin);
    //freopen("Sum.out","w",stdout);
    int T;
    init(1e6);
    read(T);
    while(T--)
    {
        scanf("%s",s+1);
        n=strlen(s+1);
        int j=0;
        for(int i=0;i<=n;i++)vec[i].clear(),buc[i]=0;
        vec[0].push_back(1);
        for(int i=2;i<=n;i++)
        {
            while(j&&s[j+1]!=s[i])j=bd[j];
            if(s[j+1]==s[i])j++;
            bd[i]=j;
            vec[bd[i]].push_back(i);
        }
        ans=1;
        dfs(0);
        printf("%d\n",ans);
    }
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2596ms
memory: 210964kb

input:

10
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

348588201
444875250
645157594
69295643
479125431
754799284
363702774
250095339
288773558
250829868

result:

ok 10 lines