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: ... 
随机推荐
- [App Store Connect帮助]二、 添加、编辑和删除用户(6)生成 API 密钥
			如果已批准您访问 App Store Connect API,您可以生成 API 密钥,以便使用该密钥配置.认证和使用 App Store Connect 服务. 有关管理和保护您密钥的更多信息,请参 ... 
- 微信小程序连接Java后台
			有人问我小程序怎么连后台,这里直接贴代码 在app.js里 // api request request(url, params) { return new Promise((resolve, rej ... 
- redis过期策略和内存淘汰机制
			目录 常见的删除策略 redis使用的过期策略:定期删除+惰性删除 定期删除 惰性删除 为什么要采用定期删除+惰性删除2种策略呢? redis内存淘汰机制 常见的删除策略 1.定时删除:在设置键的过期 ... 
- Kafka详解与总结(五)
			Kafka持久化 1. 概述 Kafka大量依赖文件系统去存储和缓存消息.对于硬盘有个传统的观念是硬盘总是很慢,这使很多人怀疑基于文件系统的架构能否提供优异的性能.实际上硬盘的快慢完全取决于使用它的方 ... 
- HTML--使用下拉列表框进行多选
			下拉列表也可以进行多选操作,在<select>标签中设置multiple="multiple"属性,就可以实现多选功能,在 widows 操作系统下,进行多选时按下Ct ... 
- Ajax实现文件的上传
			Ajax实现文件的上传 准备 ajax的参数补充 type不写的话默认是GET dataType和ContentType: dataType: 浏览器发给服务器希望返回的数据类型 .. 如果明确地指定 ... 
- DataFrame入门案例(集团公司对人事信息处理场景)
			我用一个集团公司对人事信息处理场景的简单案例,来作为入门,详细分析DataFrame上的各种常用操作,包括集团子公司的职工人事信息的合并,职工的部门相关信息查询.职工信息的统计.关联职工与部门信息的统 ... 
- [Android]异常2-Unexpected error while executing
			异常原因: 可能一>Android Studio的自动编译没有成功 解决方法有: 解决一>菜单栏里的“Build”,“Clean Project” 注: 
- 【sqli-labs】 less44 POST -Error based -String -Stacked Blind(POST型基于盲注的堆叠字符型注入)
			盲注漏洞,登陆失败和注入失败显示的同一个页面 可以用sleep函数通过延时判断是否闭合引号成功 这个方法有一点不好的地方在于,并不能去控制延时,延时的时间取决于users表中的数据数量和sleep函数 ... 
- Django 更新字段
			Django在1.7以后的版本提供数据迁移命令,用来在修改模型中的字段,更新到数据库 1. python manager.py makemigrations 命令用来创建迁移文件版本的 2. pyth ... 
