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: ...
随机推荐
- 自然常数 e 的理解与应用
某彩票中奖率是百万分之一,则一个人买一百万张彩票仍不中奖的概率是: (1−1106)106≈1e e 往往出现在: 许多微小事件带来的总体变化 随机性和无穷多:
- Android SDK Manager 无法更新问题(转载)
先看看如何加快更新速度,再说如何更新. 首先更新host文件,如图,打开目录 C:\Windows\System32\drivers\etc,在目录下有hosts文件 打开方式选用“记事本”打开 将一 ...
- Appium + python - TouchAction操作
from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom appium. ...
- html基础代码演示2
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 卸载Mysql connect 6.9.9
我们在卸载MySQL的时候,会发现有一个名为“Connector Net X.X.X”(如:Connector Net 6.9.9)软件总是卸载不成功,下面我们来看看解决方法:1. 在C盘的目录下,有 ...
- 把datagrid转换成gridview
public gridview datagrid2gridview(datagrid dg) { gridview gv = new gridview(); foreach(var p in dg.c ...
- struts2.1.6存在中文乱码的bug
如题,后续版本中已解决:可以通过添加filter的方式解决.
- android中textview单行显示,多余的省略
<TextView android:id="@+id/music_title" android:layout_width="wrap_content" a ...
- IE9的F12工具,"网络"页签,点击"开始捕获"之后,请求显示的状态是"挂起"的分析和解决
最近一个项目,客户端使用用jQuery编写ajax请求,服务端采用struts2框架.js发送请求和action处理请求过程中,遇到一个问题.刚开始觉得问题很诡异,仔细定位很久之后才发现问题,虽然问题 ...
- Redis系列(一)StackExchange.Redis的使用
Redis系列(一)StackExchange.Redis的使用 一.DLL安装 用NuGet搜索StackExchange.Redis,然后下载就可以. ConnectionMultiplexer对 ...