MG loves string
MG loves string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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.
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.
It's obvious that the expected steps X will be a decimal number.
You should output X∗26Nmod 1000000007.
2
abcdefghijklmnpqrstuvwxyzo
1
abcdefghijklmnopqrstuvwxyz
26
#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的更多相关文章
- 【HDU 6021】 MG loves string (枚举+容斥原理)
MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 6021 MG loves string
MG loves string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...
- hdu 6021 MG loves string (一道容斥原理神题)(转)
MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ●HDU 6021 MG loves string
题链: http://acm.hdu.edu.cn/showproblem.php?pid=6021 题解: 题意:对于一个长度为 N的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符 i ...
- hdu6021[BestCoder #93] MG loves string
这场BC实在是有趣啊,T2是个没有什么算法但是细节坑的贪心+分类讨论乱搞,T3反而码起来很顺. 然后出现了T2过的人没有T3多的现象(T2:20人,T3:30人),而且T2的AC率是惨烈的不到3% ( ...
- hdu 6020 MG loves apple 恶心模拟
题目链接:点击传送 MG loves apple Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Ja ...
- best corder MG loves gold
MG loves gold Accepts: 451 Submissions: 1382 Time Limit: 3000/1500 MS (Java/Others) Memory Limit ...
- (set)MG loves gold hdu6019
MG loves gold Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- 【HDU 6020】 MG loves apple (乱搞?)
MG loves apple Accepts: 20 Submissions: 693 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: ...
随机推荐
- 杂项:LDAP
ylbtech-杂项:LDAP 1.返回顶部 1. LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X. ...
- 40. combo的displayField和valueField属性
转自:https://xsl2007.iteye.com/blog/773464 下拉框combo可以设置displayField和valueField属性,这两个值值相当于Java中的map,一个键 ...
- vs打开wixproj后缀文件
1.在正常情况下vs是无法打开wixproj工程的,能打开也只能是以记事本方式打开该文件本身 2.所以此时需要下载wixtool,安装后即可打开上述类型文件 3.最好也安装好vs对应版本的扩展包 4. ...
- PCB genesis连孔加除毛刺孔(圆孔与圆孔)实现方法(一)
一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...
- native2ascii运用
1.native2ascii命令行的格式 native2ascii -[option] [inputfile [outputfile]] 说明: -[option]:表示命令开关,有两个选项可供选择: ...
- [Swift通天遁地]五、高级扩展-(12)扩展故事板中的元件添加本地化功能
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 各地图API坐标系统比较
各地图API坐标系统比较 简单记录一下各家坐标系 WGS84坐标系:即地球坐标系,国际上通用的坐标系.设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,谷歌地图采用的是WGS84地 ...
- 【BZOJ3205_洛谷3638】[APIO2013]机器人(动态规划)
题目: 洛谷3638 分析: 卡了一天的神题--(OrzJumpmelon) 首先预处理出从点\(p\)向\(d\)方向出发最终能到达的点\(nxt[p][d]\).这个可以直接记忆化搜索解决.如果出 ...
- day01_12/11/2016_Spring入门PPT
s1 s2 s3 s4 s5 s6 s7 s8 IOC1 IOC2 入门编写1 入门编写2 入门编写3 入门编写4---心得
- RabbitMQ~消费者实时与消息服务器保持通话
这个文章主要介绍简单的消费者的实现,rabbitMQ实现的消费者可以对消息服务器进行实时监听,当有消息(生产者把消息推到服务器上之后),消费者可以自动去消费它,这通常是开启一个进程去维护这个对话,它与 ...