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. Scala List 用法

    1.++[B]   在A元素后面追加B元素 scala> val a = List(1) a: List[Int] = List(1) scala> val b = List(2) b: ...

  2. Autowire

    Field userService in com.demo.web.Controller.HomeController required a single bean, but 2 were found ...

  3. RabbitMQ学习之(五)_一个基于PHP的RabbitMQ操作类

    //amqp.php类文件 <?php class Amqp { public $e_name; public $q_name; public $k_route; public $channel ...

  4. web.xml context-param配置

    context-param 为上下文初始化参数 解析:每个<context-param>元素含有一对参数名和参数值(param-name和param-value),用作应用的Servlet ...

  5. 探讨"点"语法的奥秘

    点语法 一直以来,都不理解什么是点语法,都说是相当于链接或是路径.也许我浏览的信息量少吧,看过好几篇有关的博文,没什么记载,本篇只是初步见解分析. 在javascript里,属性和方法都使用“点”语法 ...

  6. 可扩展多线程异步Socket服务器框架EMTASS 2.0 (转自:http://blog.csdn.net/hulihui)

    可扩展多线程异步Socket服务器框架EMTASS 2.0 (转自:http://blog.csdn.net/hulihui) 0 前言 >>[前言].[第1节].[第2节].[第3节]. ...

  7. js 的胖箭头问题

    我们在声明函数的时候通常是 var foo function(a){ console.log(a) }; 用ES6 我们写成了这样 var foo = a =>{ console.log(a); ...

  8. LeetCode——Range Sum Query - Immutable

    Question Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  9. Typora 配置说明

    目录 Typora 配置说明 贴图功能 自定义快捷键 快捷键使用 Linux下安装typora Typora 配置说明 为了更好的使用markdown,解决markdown中不如Word的不便之处,对 ...

  10. Python简易项目 加减计算器的实现

    Python Calculator 1.0 支持功能:add.minus 输入表达式不含括号,允许不加'=' 非常简单的一个小计算器,还缺少很多功能,目的是为了练练手. 日后会对其进行更新. 源码 G ...