hdu 6021 MG loves string
MG loves string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 131 Accepted Submission(s):
50
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 .
And as for each case, there are 1 integer 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.
line.
It's obvious that the expected steps X will be a decimal number.
You should output X∗26Nmod 1000000007 .
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int N_MAX = + ,INF= ;
int N;
string s;
vector<pair<int,int> >loop;//圈的大小<->圈的个数 ll gcd(ll a,ll b) {
if (b == )return a;
return gcd(b, a%b);
} ll lcm(ll a,ll b) {
return a / gcd(a, b)*b;
} void cal_loop() {
loop.clear();
int change[],vis[];
memset(vis,,sizeof(vis));
for (int i = ; i < s.size();i++) {
change[i] = s[i] - 'a';
}
map<int, int>m;
for (int i = ; i < ;i++) { if (vis[i])continue;
vis[i] = true;
int num = ;
int k = change[i];//k代表不断变化的字母
while (i != k) {
vis[k] = true;
num++;//该圈的元素个数加1
k = change[k];//!!!!!顺序
}
m[num]++;
}
for (map<int, int>::iterator it = m.begin(); it != m.end();it++) {
loop.push_back(*it);
}
} ll mod_pow(ll x,ll n) {//快速幂运算
ll res = ;
while (n>) {
if (n & )res = res*x%INF;
x = x*x%INF;
n >>= ;
}
return res;
} ll R_C(vector<int>&loop,int N) {//容斥原理,求由这些圈中的元素组成的长度为N的字符串的数量
ll permute = << (loop.size());
ll ans = ;
for (int i = ; i < permute;i++) {
int num = ;
int sum = ;
int sign=-;
for (int j = ; j < loop.size(); j++) {
if (i&( << j)) {
num++;//num记录利用到的圈的个数
sum += loop[j];//利用到的字符的总个数
}
}
if (num % == loop.size() % )//!!!!!!!!
sign = ;
ans =(ans+((sign*mod_pow(sum, N))%INF+INF)%INF)%INF;
}
return ans;
} ll solve(int N) {
cal_loop();
vector<int>vec;
ll ans = ;
for (int i = ; i < (<<loop.size());i++) {
ll change_time=;
vec.clear();
for (int j = ; j < loop.size();j++) {
if (i&( << j)) {
vec.push_back(loop[j].first*loop[j].second);
change_time = lcm(change_time, loop[j].first);
}
}
if (vec.size() > N)continue;//挑选出来的圈的个数不能超过字符串长度
ll number = R_C(vec, N);
ans = (ans + change_time*number) % INF;
}
return ans;
} int main() {
int T;
scanf("%d",&T);
while (T--) {
scanf("%d",&N);
cin >> s;
printf("%lld\n",solve(N));
}
return ;
}
hdu 6021 MG loves string的更多相关文章
- 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 ...
- 【HDU 6021】 MG loves string (枚举+容斥原理)
MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- MG loves string
MG loves string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...
- hdu 6020 MG loves apple 恶心模拟
题目链接:点击传送 MG loves apple Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Ja ...
- 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 Accepts: 20 Submissions: 693 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: ...
- best corder MG loves gold
MG loves gold Accepts: 451 Submissions: 1382 Time Limit: 3000/1500 MS (Java/Others) Memory Limit ...
- Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)
Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...
随机推荐
- String中关于BeanFactory
org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...
- iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- 从 Objective-C 里的 Alloc 和 AllocWithZone 谈起
一.问题起源 一切起源于Apple官方文档里面关于单例(Singleton)的示范代码:Creating a Singleton Instance.主要的争议集中在下面这一段: static MyGi ...
- linux下的一些命令分析与shell的一些命令
对> 与 >>的理解 echo "aaa" > aaa.txt 这个是在aaa.txt中写入aaa 可以用cat aaa.txt查看 echo &qu ...
- python 列表加法"+"和"extend"的区别
相同点 : "+"和"extend"都能将两个列表成员拼接到到一起 不同点 : + : 生成的是一个新列表(id改变) extend : 是将一个列表的成员 ...
- Python9-进程-day36
import osfrom multiprocessing import Processimport timedef func(args,args2): print(args,args2) time. ...
- FSMC原理通俗解释
所以不用GPIO口直接驱动液晶,是因为这种方法速度太慢,而FSMC是用来外接各种存储芯片的,所以其数据通信速度是比普通GPIO口要快得多的.TFT-LCD 驱动芯片的读写时序和SRAM的差不多,所以就 ...
- 自动设置IP地址bat脚本
自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...
- JAVA 基础--开发环境Sublime Text 3 搭建
方法一 打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...
- day03 set集合,文件操作,字符编码以及函数式编程
嗯哼,第三天了 我们来get 下新技能,集合,个人认为集合就是用来list 比较的,就是把list 转换为set 然后做一些列表的比较啊求差值啊什么的. 先看怎么生成集合: list_s = [1,3 ...