HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))
Invoker
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 122768/62768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
In his new map, Kael can control n kind of elements and he can put m elements equal-spacedly on a magic ring and combine them to invoke a new skill. But if a arrangement can change into another by rotate the magic ring or reverse the ring along the axis, they will invoke the same skill. Now give you n and m how many different skill can Kael invoke? As the number maybe too large, just output the answer mod 1000000007.
Input
For each test case: give you two positive integers n and m. ( 1 <= n, m <= 10000 )
Output
Sample Input
2
3 4
1 2
Sample Output
Case #1: 21
Case #2: 1
Hint
Here are the 21 different arrangements to invoke the skills
/ aaaa / aaab / aaac / aabb / aabc / aacc / abab /
/ abac / abbb / abbc / abcb / abcc / acac / acbc /
/ accc / bbbb / bbbc / bbcc / bcbc / bccc / cccc /
Source
乘法逆元:http://blog.csdn.net/yukizzz/article/details/51105009
乘法逆元什么用:
若对于数字A,C 存在X,使A * X = 1 (mod C) ,那么称X为 A 对C的乘法逆元。
12 / 4 mod 7 = ? 很显然结果是3
我们现在对于数对 (4,7), 可以知道 X = 2是 4 对7的乘法逆元即2*4=1(mod 7)
那么我们有(12 / 4) * (4 * 2 ) = (?) * (1) (mod 7)
除法被完美地转化为了乘法。
1. 用了费马小定理+快速幂 求 乘法逆元
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std;
int T;
const long long mod=;
long long res,n,m;
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
long long poww(long long a,long long b)
{
long long ans=;
while(b)
{
if (b%==) ans=(ans*a)%mod;
a=(a*a)%mod;
b/=;
}
return ans;
}
int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++)
{
scanf("%lld%lld",&m,&n);
res=;
for(long long i=;i<n;i++)
res=(res+poww(m,gcd(n,i)))%mod;
if(n%==)
res=(res+poww(m,(n+)/)*n)%mod;
else
{
res=(res+poww(m,n/+)*(n/))%mod;
res=(res+poww(m,n/)*(n/))%mod;
}
printf("Case #%d: ",t);
printf("%lld\n",(res*poww(*n,mod-))%mod);
}
return ;
}
2.用扩展欧几里德求逆元
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std;
int T;
const long long mod=;
long long res;
int m,n; int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
long long poww(long long a,int b)
{
long long ans=;
while(b)
{
if (b&) ans=(ans*a)%mod;
a=(a*a)%mod;
b/=;
}
return ans;
} //返回d=gcd(a,b);和对应于等式ax+by=d中的x,y
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
if(a==&&b==) return -;//无最大公约数
if(b==){x=;y=;return a;}
long long d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
//*********求逆元素*******************
//ax = 1(mod n)
long long mod_reverse(long long a,long long n)
{
long long x,y;
long long d=extend_gcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
} int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++)
{
scanf("%d%d",&m,&n);
res=;
for(int i=;i<n;i++)
res=(res+poww(m,gcd(n,i)))%mod;
if(n%==)
res=(res+poww(m,(n+)/)*n)%mod;
else
{
res=(res+poww(m,n/+)*(n/))%mod;
res=(res+poww(m,n/)*(n/))%mod;
}
printf("Case #%d: ",t);
printf("%I64d\n",(res*mod_reverse(*n,mod))%mod);
}
return ;
}
HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))的更多相关文章
- HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Outp ...
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7). 当中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)
M斐波那契数列 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理
题目描述 求 $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...
- [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)
Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...
- 简记乘法逆元(费马小定理+扩展Euclid)
乘法逆元 什么是乘法逆元? 若整数 \(b,m\) 互质,并且\(b|a\) ,则存在一个整数\(x\) ,使得 \(\frac{a}{b}\equiv ax\mod m\) . 称\(x\) 是\( ...
随机推荐
- spark 作业提交
kafka-topics.sh --describe --zookeeper xxxxx:2181 --topic testkafka-run-class.sh kafka.tools.GetOffs ...
- 论文笔记:空间变换网络(Spatial Transformer Networks)
2015, NIPS Max Jaderberg, Karen Simonyan, Andrew Zisserman, Koray Kavukcuoglu Google DeepMind 为什么提出( ...
- Extjs 正则表达式 常用的
extjs正则表达式验证 2011年10月10日 10:36:05 阅读数:7305 在EXT中使用正则表达式验证的方法:fieldLabel : '员工号',name : 'employee.e ...
- iOS 自动订阅开发
一.代码逻辑 关于iOS 订阅.自动订阅 本身功能开发很简单.跟正常的购买没什么大的差异.唯一需要特殊处理(自动订阅)的是, 在APP启动时候要增加侦听: [[SKPaymentQueue defau ...
- 一幅图秒懂LoadAverage(转载)
转自:http://www.habadog.com/2015/02/27/what-is-load-average/ 一幅图秒懂LoadAverage(负载) 一.什么是Load Average? ...
- C++ 单链表的实现
#ifndef LINKEDLIST_H #define LINKEDLIST_H #define MAXLEN 256 template <class T> struct LinkedL ...
- Visual Studio 2015 初体验
据微软介绍每次发布的新版本,都承载着为开发者提供最高效的Visual Studio开发体验的使命.Visual Studio 2015亦延续了这一趋势,为开发者带来了进一步的生产力创新,包括调试和诊断 ...
- https nginx配置
cd /saas/conf/nginx/ mkdir key cd key 创建key: openssl req -nodes -newkey rsa:2048 -keyout server.key ...
- Visual Studio 2010生成解决方案时,导致C盘空间越来越小
为了从根本上解决问题,还是去掉智能跟踪选项吧,方案: VS2010-->工具-->选项-->IntelliTrance-->将“启用IntelliTrace”勾选去掉--> ...
- linux安装coreseek
coreseek就是一个中文词库加上sphinx组合而成的. 1.下载coreseek 下载到/usr/local/src目录文件下 wget http://www.coreseek.cn/uplo ...