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. SSIS 2012 Error: An Integration Services class cannot be found

    升级SSIS到SQL Server 2012,服务器只安装了SSIS一个功能,应用程序执行dtsx包时报错如下: An Integration Services class cannot be fou ...

  2. C++之旅:拷贝构造与友元

    拷贝构造与友元 拷贝构造是在构造一个对象的时候将已有对象的属性拷贝给新的对象:友元可以让一个类的所有属性(主要是private)对特定的类开放 拷贝构造 如果没有复写拷贝构造函数,系统会帮我们默认生成 ...

  3. 利用URLConnection来发送POST和GET请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  4. Spark高级数据分析· 3推荐引擎

    推荐算法流程 推荐算法 预备 wget http://www.iro.umontreal.ca/~lisa/datasets/profiledata_06-May-2005.tar.gz cd /Us ...

  5. Web前端学习笔记之JavaScript、jQuery、AJAX、JSON的区别

    官网的英文解释: javascript和jQuery有点关系,js是一种脚本语言,主要用于客户端,现在主要用于实现一些网页效果. jquery是js的一个库,你可以认为是对js的补充,提供了很多方便易 ...

  6. Centos下给PHP7添加Xhprof性能分析

    什么是 Xhprof?XHProf是facebook 开发的一个测试php性能的扩展,本文记录了在PHP应用中使用XHProf对PHP进行性能优化,查找性能瓶颈的方法. 它报告函数级别的请求次数和各种 ...

  7. 生产者消费者JAVA实现

    三种实现方式: 1. Object对象的wait(),notify(),加synchronize. 2. Lock的await(),signal(). 3. BlockingQueue阻塞队列. Ob ...

  8. Linux ASLR的实现

    ASLR大家都会听说过,但是Linux平台下应用程序的ASLR的情况是怎么样的呢?我在这里将ASLR分为几个小的部分来阐述,包括了栈的随机化,堆的随机化,mmap的随机化,以及pie程序运行时的主模块 ...

  9. scrapy之手机app抓包爬虫

    手机App抓包爬虫 1. items.py class DouyuspiderItem(scrapy.Item): name = scrapy.Field()# 存储照片的名字 imagesUrls ...

  10. 2017 ACM/ICPC Asia Regional Shenyang Online array array array

    2017-09-15 21:05:41 writer:pprp 给出一个序列问能否去掉k的数之后使得整个序列不是递增也不是递减的 先求出LIS,然后倒序求出最长递减子序列长度,然后判断去k的数后长度是 ...