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

On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful.

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

The first line contains a single positive integer T( T <= 500 ), indicates the number of test cases.
For each test case: give you two positive integers n and m. ( 1 <= n, m <= 10000 )

Output

For each test case: output the case number as shown and then output the answer mod 1000000007 in a line. Look sample for more information.

Sample Input

2
3 4
1 2

Sample Output

Case #1: 21
Case #2: 1

Hint

For Case #1: we assume a,b,c are the 3 kinds of elements.
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

2011 Multi-University Training Contest 9 - Host by BJTU

乘法逆元: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定理+乘法逆元(扩展欧几里德+费马小定理))的更多相关文章

  1. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

  3. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

  4. hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7). 当中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                 ...

  5. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  6. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  7. 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理

    题目描述 求  $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...

  8. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  9. 简记乘法逆元(费马小定理+扩展Euclid)

    乘法逆元 什么是乘法逆元? 若整数 \(b,m\) 互质,并且\(b|a\) ,则存在一个整数\(x\) ,使得 \(\frac{a}{b}\equiv ax\mod m\) . 称\(x\) 是\( ...

随机推荐

  1. [转]Algolia的分布式搜索网络架构

    转自:http://www.csdn.net/article/2015-03-11/2824176-the-architecture-of-algolias-distributed-search-ne ...

  2. java中boolean类型占几个字节

    java的基本数据类型中,boolean只有两种状态,默认值为false.取值范围是{true,false},理论上占1bit,实际上: 1.单个的boolean 类型变量在编译的时候是使用的int ...

  3. union遇上ntext数据类型

    http://www.myhack58.com/Article/html/3/7/2011/31392.htm

  4. vue2.0中配置文件路径

    在build/webpack.base.conf.js中添加一些代码即可 module.exports = { resolve: { extensions: ['.js', '.vue', '.jso ...

  5. 安装centos7最小化安装

    分享一篇不错的文章: https://www.howtoforge.com/tutorial/centos-7-server/

  6. python-内置函数及捕获异常

    eval:把字符串转换成有效表达式 repr:把有效表达式转换成字符串 round(...) round(number[, ndigits]) -> number     Round a num ...

  7. 建站有很多技术,如 HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、http://ASP.NET、Web Services、浏览器脚本、服务器脚本等。它们的区别是什么?新手一点不懂,想理清所有这些技术之间的关系和应用范围。

    先普及用户通过 浏览器 访问网页 的过程: 网页内容是通过服务器运算得出的结果,将结果(网页代码)传输给浏览器,网页代码再通过浏览器运算(计算.渲染),最终展示在用户的眼前的. 至此,我们知道了有2个 ...

  8. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  9. [PyTorch]PyTorch/python常用语法/常见坑点

    目录 1. make_grid() 2. join与os.path.join() 3. 读文件写文件 4. json操作 5. tensorboard使用 6. python shutil.move ...

  10. HDFS读写流程learning

    有许多对流程进行描述的博客,但是感觉还是应当学习一遍代码,不然总感觉怪怪的,https://blog.csdn.net/popsuper1982/article/details/51615285,首先 ...