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> # ...
随机推荐
- 说说TCP的三次握手
在说这个问题之前,先说说IP协议和TCP协议 问题:IP协议能做什么?不能做什么? 我们都知道IP协议是无连接的通信协议,它不会占用两个正在通信的计算机的通信线路,这样就降低了IP对网络传输中的需求, ...
- javaweb基础(22)_Servlet+JSP+JavaBean实战登陆
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- python-下拉框处理
在自动化中python对下拉框的处理网上相对实例比较少,其它前辈写的教程中对下拉也仅仅是相对与教程来说的,比如下面: m=driver.find_element_by_id("Shippin ...
- Seek and Destroy-freecodecamp算法题目
Seek and Destroy(摧毁数组) 要求 实现一个摧毁(destroyer)函数,第一个参数是待摧毁的数组,其余的参数是待摧毁的值. 思路 利用for循环将输入arguments中除第一项待 ...
- MongoDB - 启动&连接数据库
1> 启动数据库 1.1> 依次添加如下目录: 1.1.1> mongodb-space 1.1.2> mongodb-space/conf 1.1.3> mongodb ...
- 02Vs2013常用路径配置
1.设置头文件路径 项目 -> xxx属性页 -> 配置属性 -> C/C++ -> 常规 -> 附加包含目录. 2.包含 x.lib 库路径 项目 -> xxx属 ...
- html页面简单访问限制
PS:突然发现博客园有密码保护功能,已经可以满足基本需求了.博客园还能备份自己的所有数据,做到了数据归用户所有,平台只是展示,真是良心网站,大赞. 想要通过一个站点放一些东西给一些人看,但是又不想让所 ...
- vue-highlightjs的使用小结
万能的github真主,让我们强大!在vue的项目中想使用highlight.js这样的代码高亮?有人帮助我们实现了vue-highlightjs 安装 yarn add highlight.js - ...
- MySQL中CONCAT()的用法
MySQL中CONCAT()的用法 在日常开发过程中,特别是在书写接口的时候,经常会遇到字符串拼接的情况,比如在返回图片数据时,数据库里往往存储的是相对路径,而接口里一般是存放绝对地址,这就需要字符串 ...
- Python简单试题3
1,水仙花数 水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身 (例如:1^3 + 5^3+ 3^3 = 153) 代码如下: 方法一: for i in range(100, ...