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\) 是\( ...
随机推荐
- C++ vector 用法
转自http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html#undefined 在c++中,vector是一个十分有用的容器,下面对这 ...
- laravel request 增加字段
https://segmentfault.com/q/1010000006898668 $input = $request->only(['username', 'password']); // ...
- H5 动画:轨迹移动 | H5游戏 推金币
https://aotu.io/notes/2017/11/06/path-animation/ https://aotu.io/notes/2017/11/06/coindozer/
- Linux学习笔记之Xshell配色方案定制
点击 Xshell 面板顶部的如下按钮. 点击 Browse 按钮,弹出如下面板,选择 ANSI Colors on Black,然后点击右侧save as 按钮,命名为 zkl. 这里其实就是复 ...
- Windows Update error 80070003
上次更新完成一半,这次更新便会出错.办法:删除上次更新残余文件. 删除Windows 用于标识计算机更新的临时文件.需要先停止Windows Update 服务: 在开始菜单的“搜索程序和文件”框输入 ...
- 20145314郑凯杰 《Java程序设计》实验四 实验报告
20145314郑凯杰 <Java程序设计>实验四 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- center os7.2 apache+php+mysql环境配置并设置https访问
本人阿里云购买的center os7.2系统,小程序只支持https,因此需要配置https 安装apache yum -y install httpd systemctl start httpd a ...
- struts1.2上传多个文件
页面: <input type="file" name="impFile[0]" style="wid ...
- nuget sources
https://docs.microsoft.com/en-us/nuget/tools/cli-ref-sources https://gemfury.com/help/nuget-server/ ...
- MySQL INSERT语句
insert的语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] ...