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> # ...
随机推荐
- java利用SuffixFileFilter统计目录下特定后缀名文件的数目
/** * 文件处理类 * @author zhangcd * @date 2017年1月3日 */ public class FileUtil { /** * 得到所有后缀的数目 * * @para ...
- (转发)InputAccessoryView的使用方法
转自:http://blog.sina.com.cn/s/blog_45e2b66c01015we9.html UITextFields and UITextViews have an inputAc ...
- bzoj4666 小z的胡话
题目描述: bz 题解: 乱搞好题哇. 众所周知斐波那契数列是有循环节的. 我们可以搞出在模$10^x$下与所给得数同余的集合,那么在模$10^{x+1}$下,同余集合一定是原集合及循环若干循环节的大 ...
- 15Shell脚本—流程控制
流程控制语句 尽管可以通过使用Linux命令.管道符.重定向以及条件测试语句编写最基本的Shell脚本,但是这种脚本并不适用于生产环境.原因是它不能根据真实的工作需求来调整具体的执行命令,也不能根据某 ...
- vue-cli webpack配置cdn路径 以及 上线之后的字体文件跨域处理
昨天搞了一下vue项目打包之后静态资源走阿里云cdn. 配置了半天,终于找到了设置的地方 config/index.js 里面设置build 下的 assetsPublicPath 打包的时候便可以添 ...
- vue渲染函数&JSX
Vue推荐在绝大多数情况下使用template来创建你的HTML.然而在一些场景中,你真的需要JavaScript的完全编程能力,这时你可以使用render函数,它比template跟接近编译器. 虚 ...
- 如何在 CentOS 7 上安装 Python 3
当前最新的 CentOS 7.5 默认安装的是 Python 2.7.5,并且默认的官方 yum 源中不提供 Python 3 的安装包.这里主要介绍两种在 CentOS 7 中安装 Python 3 ...
- 【php】instanceof
instanceof 的使用还有一些陷阱必须了解.在 PHP 5.1.0 之前,如果要检查的类名称不存在,instanceof 会调用__autoload().另外,如果该类没有被装载则会产生一个致命 ...
- vagrant 安装ubuntu12.04 64 bit
1 下载用于ubuntu 12.04 用于vagrant的镜像,虚拟机是virtualbox $ wget http://files.vagrantup.com/precise64.box jb@e3 ...
- Linux学习-延伸正则表达式
grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...