MG loves string

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

Problem Description
MG 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.

 
Input
The 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.

 
Output
As 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 Input
2
2
abcdefghijklmnpqrstuvwxyzo
1
abcdefghijklmnopqrstuvwxyz
 
Sample Output
5956
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 ;
}

MG loves string的更多相关文章

  1. 【HDU 6021】 MG loves string (枚举+容斥原理)

    MG loves string  Accepts: 30  Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  2. hdu 6021 MG loves string

    MG loves string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others ...

  3. hdu 6021 MG loves string (一道容斥原理神题)(转)

    MG loves string    Accepts: 30    Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. ●HDU 6021 MG loves string

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=6021 题解: 题意:对于一个长度为 N的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符 i ...

  5. hdu6021[BestCoder #93] MG loves string

    这场BC实在是有趣啊,T2是个没有什么算法但是细节坑的贪心+分类讨论乱搞,T3反而码起来很顺. 然后出现了T2过的人没有T3多的现象(T2:20人,T3:30人),而且T2的AC率是惨烈的不到3% ( ...

  6. hdu 6020 MG loves apple 恶心模拟

    题目链接:点击传送 MG loves apple Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Ja ...

  7. best corder MG loves gold

    MG loves gold  Accepts: 451  Submissions: 1382  Time Limit: 3000/1500 MS (Java/Others)  Memory Limit ...

  8. (set)MG loves gold hdu6019

    MG loves gold Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  9. 【HDU 6020】 MG loves apple (乱搞?)

    MG loves apple  Accepts: 20  Submissions: 693  Time Limit: 3000/1500 MS (Java/Others)  Memory Limit: ...

随机推荐

  1. [ZJOI 2009] 假期的宿舍

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1433 [算法] 二分图匹配[代码] #include<bits/stdc++. ...

  2. [BZOJ 2006] 狼抓兔子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1001 [算法] 最小割 [代码] #include<bits/stdc++.h ...

  3. C# MySql 连接

    1.将MySql.Data.dll引用到你的项目中 右键工程去完成. 2.using MySql.Data;  using MySql.Data.MySqlClient; 3. MySqlConnec ...

  4. VPS主机

    腾讯云企业认证 备案域名要是顶级域名格式,所以不能是www.blueheartzf.com,而要是blueheartzf.com

  5. int(3)和int(11)区别

  6. J2EE集群原理(摘录)

    J2EE集群原理 什么是集群呢?总的来说,集群包括两个概念:“负载均衡”(load balancing)和“失效备援”(failover)  图一:负载均衡 多个客户端同时发出请求,位于前端的负载均衡 ...

  7. CSS——个人资料demo

    1.上下外边距合并,选最大值. 2.两个input标签在编辑中如果换行了,在浏览器中显示的时候会自动增加一些距离. <!DOCTYPE html> <html lang=" ...

  8. strut2 拦截器 使用

    拦截器是strut2里一个很振奋人心的应用.通过配置拦截器可以在action执行之前进行一些初始化或者是其他的操作,但是在action执行之后,返回结果就已经确定,结果是很难改变了(目前我还不知道怎么 ...

  9. C 语言常用方法技巧

    C语言常用方法技巧 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...

  10. vi 命令学习(一)

    行内移动] w word 向后移动一个单词 b back ·向前移动一个单词 行首 ^ 行首,第一个不是空白字符的位置 $ 行尾 [行数移动] gg go 文件顶部 G go 文件末尾 数字gg go ...