Invoker



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

解题思路:

Polya计数。题目可转化为用c种颜色给n个珠子的项链染色,问一共同拥有多少种颜色方案。本题要对结果取模1000000007

1.旋转。

将环顺时针旋转i格后,循环节个数为gcd(n,i), 染色方案为  ∑c^gcd(n,i)    当中 i=1,2,3,4,....n

2.翻转。

这里也得考虑两种情况。

当n为奇数时。共同拥有n个循环节个数为(n/2+1)的循环群,还有的资料上说是环的个数为(n/2+1)
,注意这是计算机上的表示,n/2整型相除计算机得到的是整数。事实上应该写成(n+1)/2。。染色方案为  n*c^(n/2+1)

为什么n个循环节个数为(n/2+1)的循环群呢?我的理解是这种,也许不太对。。。

拿正三角形为例,给它三个顶点染色, 对称轴是一个顶点与其对边终点连线所在的直线,这种直线有3(n=3。即n个顶点) 条,共同拥有3(n)个循环群。

如果第一个顶点在对称轴上,那么第二个顶点经过对称轴翻转肯定和第三个顶点重合,那么 (2,3)是一个循环节。(1)自己是一个循环节,循环节个数为2,即(n+1/2)。

当n为偶数时,共同拥有n个循环群。当中有n/2个的循环节个数为(n/2 +1)。 有n/2个的循环节个数为(n/2)。

拿正方形为例。四个顶点从左上角顺时针编号1,2,3,4.

当以1,3顶点连线所在直线为对称轴时(对角的两个顶点),这样对称轴有2个(n/2)。经过翻转。2,4 重合,1和1重合,3和3重合。那么循环节的个数为3(2,4) (1)(3), 即(n/2+1)。 染色方案为
 (n/2)*c^(n/2+1)

当以两条相对平行的边的中点连线所在直线为对称轴时,比方以线段1,2的中点和3,4的中点连线的所在直线为对称轴。这种对称轴有两个(n/2),经过翻转。1,2重合,3,4重合,循环节的个数为2。(1,2)(3,4),即(n/2)。,也就是谁和谁重合。谁就和谁在一个循环节里。

染色方案为(n/2)*c^(n/2)



最后累加方案得到ans, 再除以置换群的个数2*n,即 ans/(2*n)%mod即为最后答案。但这里要特别注意,ans是在计算过程中不断取模得到的数。ans,2*n都在模剩余系中,不能直接參与除法计算,由于有公式a*b%mod=(a%mod*b%mod)%mod,除法对取余不满足结合律,a/b!=((a%mod)/(b%mod))%mod
,在计算 ans/(2*n)%mod时。能够转化为 ans*inv(2*n)%mod ,当中  inv(2*n)是2*n关于mod的逆元,保证乘以inv(2*n)和除以 2*n 对于最后的答案取余mod是一样。 

所以如今的问题是如何求一个数关于模P的逆元。



方法1:扩展欧几里得。  ax=1(mod P), gcd(a,p)=1。 当中x为a的逆元,就是我们所求,ax=PY+1,   ax-Py=1,  所以用扩展欧几里得能够求出x。

方法2:费马小定理:  假设模P是素数的话,那么inv(a)=pow(a,p-2)%p; 等式右边用高速幂运算能够得出。

參考:http://www.xuebuyuan.com/1394391.html



代码:

#include <iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
LL c,n; LL gcd(LL a,LL b)
{
return b==0? a:gcd(b,a%b);
} LL power(LL p,LL n)//高速幂运算
{
LL ans=1;
while(n)
{
if(n&1)
ans=ans*p%mod;
p=p*p%mod;
n/=2;
}
return ans;
}
LL exgcd(LL a,LL b,LL &x,LL &y)//扩展欧几里得算法。返回a,b的最大公约数,ax+by=gcd(a,b),x,y为方程的一组解
{
if(b==0)
{
x=1;
y=0;
return a;
}
long long d=exgcd(b,a%b,x,y);
long long t=x;
x=y;
y=t-a/b*y;
return d;
} int main()
{
int t;cin>>t;
int cas=1;
while(t--)
{
cin>>c>>n;
int ans=0;
for(LL i=1;i<=n;i++)
{
ans+=power(c,gcd(n,i));
ans%=mod;
}
if(n&1)
ans+=(n*power(c,n/2+1))%mod;
else
ans+=((n/2*power(c,n/2+1))%mod+(n/2*power(c,n/2))%mod)%mod; //注意mod的位置
ans%=mod;
LL x,y;
exgcd(2*n,mod,x,y);
//x=power(2*n,mod-2)%mod;//另外一种方法
x=(x+mod)%mod;
cout<<"Case #"<<cas++<<": "<<ans*x%mod<<endl;
}
return 0;
}







[ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)的更多相关文章

  1. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

  2. BZOJ 3240 [Noi2013]矩阵游戏 ——费马小定理 快速幂

    发现是一个快速幂,然而过不去. 怎么办呢? 1.十进制快速幂,可以用来练习卡时. 2.费马小定理,如果需要乘方的地方,可以先%(p-1)再计算,其他地方需要%p,所以需要保存两个数. 然后就是分类讨论 ...

  3. HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))

    Invoker Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 122768/62768K (Java/Other) Total Subm ...

  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 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

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

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

  7. hdu 4704 Sum(组合,费马小定理,快速幂)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4704: 这个题很刁是不是,一点都不6,为什么数据范围要开这么大,把我吓哭了,我kao......说笑的, ...

  8. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  9. HDU4869:Turn the pokers(费马小定理+高速幂)

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

随机推荐

  1. Python数据整合与数据准备-BigGorilla实例应用

    参考文档:http://www.biggorilla.org/walkt/ 一.BigGorilla应用主要步骤 如下图: 二.实例应用 1.数据获取 urllib是非常受欢迎的用于在网络上读取数据的 ...

  2. java源码阅读LinkedBlockingQueue

    1类签名与简介 public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements Blocking ...

  3. 【Python3 爬虫】12_代理IP的使用

    我们在爬取页面的时候,如果长时间使用一个网址去爬取某个网站,就会受爬去限制,此时,我们引用了代理IP,IP随时在变化,也就不会被限制了 一下是国内提供免费代理IP的地址:http://www.xici ...

  4. ftp mybatis

    c# 字符串和Ascii码转换http://www.cnblogs.com/JoshuaDreaming/archive/2010/11/19/1882068.html ftp 公式 http://w ...

  5. JavaScript在IE浏览器和Firefox浏览器中的差异总结

    JavaScript在IE浏览器和Firefox浏览器中存在一些差异,以下对这些差异部分进行了总结,以及解决方案: 1.HTML对象的 id 作为对象名的问题 IE:HTML 对象的 ID 可以作为 ...

  6. go项目布局(摘录)

    go的项目结构布局 或 包结构布局 这一块大家似乎还在摸索吧, 常用的应该还是类似于java的mvc布局, 但网上也有不同的布局方式,查阅github上的一些源码,也有大量的采用. 我把自己碰到的资料 ...

  7. ORCAD常见DRC错误

    一下就是网上整理的: https://blog.csdn.net/weixin_39671078/article/details/85344762 https://wenku.baidu.com/vi ...

  8. Hive 正则匹配函数

    正则匹配字符解释: ^ 表示开头 $ 表示结尾 . 表示任意字符 * 表示任意多个 regexp_extract函数 语法:    regexp_extract(string subject,  st ...

  9. unity, UGUI Image shader

      Image组件的Material成员默认是空,如果想为Image添加shader,只需新建material赋给Material即可. 另外注意,用于UI组件的shader都要包含一句:ZTest  ...

  10. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...