题意:
称一对字符串(A,B)是相似的,当且仅当满足以下条件:
(1)字符串A和B都恰好包含N个字符;
(2)A和B串中的每个字符都是小写字母的前k个字符,即A、B中只可能出现’a’,’b’,’c’,…,(’a’+k-1)这k个字符;
(3)存在一个字符串C,满足:A+C=C+B。这里的“+”号表示字符串间的链接,即str1+str2 = str1str2,如:“aaa”+“csd”=“aaacsd”。
例如,N=3,k=4那么(”aad”,”daa”)就是相似字符串对。
因为C=”aa”时,有”aad”+”aa”=”aadaa”=”aa”+”daa”.
现在给出N与k,问有多少种不同的相似字符串对,输出这个结果 mod 1,000,000,007的值。
说明:两个字符串对(A,B)与(C,D)是不同的,只要 A!=C 或 B!= D。
例如:N=2,k=2,一共有6种不同的相似字符串对,它们是: (“aa”, “aa”), (“ab”, “ab”),
(“ab”, “ba”), (“ba”, “ab”), (“ba”, “ba”), (“bb”, “bb”).
题解:
题意就是问你有多少组串是循环重构的,即A=C+G,B=G+C。
想了个假的循环节,于是越走越远……
显然一个串不重复的重构串=最小循环解长度。
然后直接容斥+快速幂就好了。
code:
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const LL mod=1000000007;
map <LL,LL> f;
LL n,k,ans=0;
LL pow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;b>>=1;
}
return ans;
}
LL solve(int x)
{
if(f[x]!=0) return f[x];
LL &g=f[x];g=pow(k,x);
if(x==1) return g;
for(int i=1;i*i<=x;i++)
{
if(x%i==0)
{
g-=solve(i);(g+=mod)%=mod;
if(i*i!=x&&i!=1) g-=solve(x/i);(g+=mod)%=mod;
}
}
return g;
}
int main()
{
scanf("%lld %lld",&n,&k);
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
ans+=i*solve(i)%mod;ans%=mod;
if(i*i!=n) ans+=n/i*solve(n/i)%mod,ans%=mod;
}
}
printf("%lld",ans);
}