首页 技术 正文
技术 2022年11月20日
0 收藏 874 点赞 4,797 浏览 2368 个字

MG loves string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem DescriptionMG is a busy boy. And today he’s burying himself in such a problem:

For a length of N, a random string made of lowercase letters, every time when it transforms, all the character i will turn into a[i].

MG states that the a[i] consists of a permutation .

Now MG wants to know the expected steps the random string transforms to its own.

It’s obvious that the expected steps X will be a decimal number.
You should output X∗26Nmod 1000000007.  InputThe first line is an integer T which indicates the case number.(1<=T<=10)

And as for each case, there are 1 integer N in the first line which indicate the length of random string(1<=N<=1000000000).

Then there are 26 lowercase letters a[i] in the next line.

 OutputAs for each case, you need to output a single line.

It’s obvious that the expected steps X will be a decimal number.
You should output X∗26Nmod 1000000007.  Sample Input2
2
abcdefghijklmnpqrstuvwxyzo
1
abcdefghijklmnopqrstuvwxyz Sample Output5956
26 分析:首先,这些字母作映射变换,构成若干个封闭的环;   其次,环的大小的不同个数不超过6个,因为1+2+3+4+5+6+7>26;   然后按环的大小分类,得到数组b,表示需要变换i次的字母个数;   接着状压枚举,问题转化为n个数满足若干不相交集合中每个集合中至少一个元素出现过的方案数;   而这个问题容斥解决;代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,a[],b[],qu[],id[],tot;
bool vis[];
char s[];
ll ret;
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
memset(vis,false,sizeof(vis));
memset(id,-,sizeof(id));
ret=;
tot=;
scanf("%d%s",&n,s);
for(i=;i<;i++)
{
if(!vis[i])
{
int cnt=;
int now=i;
while(!vis[now])cnt++,vis[now]=true,now=s[now]-'a';
if(id[cnt]==-)a[tot]=cnt,b[tot]=cnt,id[cnt]=tot++;
else b[id[cnt]]+=cnt;
}
}
for(i=;i<(<<tot);i++)
{
ll lc=;
int cnt=;
for(j=;j<tot;j++)
{
if(i>>j&)lc=lc/gcd(a[j],lc)*a[j],qu[cnt++]=j;
}
ll tmp=;
for(j=;j<(<<cnt);j++)
{
int now=,num=;
for(k=;k<cnt;k++)
{
if(j>>k&)num++,now+=b[qu[k]];
}
if((cnt-num)&)tmp=(tmp-qpow(now,n)+mod)%mod;
else tmp=(tmp+qpow(now,n))%mod;
}
(ret=ret+lc*tmp%mod)%=mod;
}
printf("%lld\n",ret);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894