HDU 3923 Invoker
Invoker
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Others)Total Submission(s): 2075 Accepted Submission(s): 950
题目链接——> 点击打开链接
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.
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 )
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
Recommend
Problem Idea
【题意】
题意为:给定n种元素,并且他可以将m个元素等间距地放置在一个魔法环上,并将它们结合起来以调用一项新技能。 但是如果一个装置可以通过旋转魔戒或者沿着轴逆转戒指而变成另一种装备,他们将会调用相同的技能。 现在给你n和m卡尔可以调用多少种不同的技能? 由于该数字可能太大,只需输出答案mod 1000000007即可。
输入:第一行是N和M,分别表示元素种类和元素个数 输出:手镯总数
【类型】
数论——等价类计数问题。组合数学,Polya定理
【分析】
等价类计数问题模板题,入门。
项链是只可以旋转,不可以翻转的。
手镯是既可以旋转,又可以翻转的。
本题魔法环上的元素可以“逆转”,也就是本题是一个求手镯数的问题
【注意】
第一次写模板,找了好长时间发现的问题:
(1)#define LL __int64,int64前面是两个短小的下划线。
(2)gcd采用gcd2的模板,gcd1不可用。为什么?(暂时我也不晓得)
(3)rotate()返回旋转时的不动点数a,overturn(),返回翻转时的不动点数b
最后根据两者之和算手镯总数,(a+b)/2/m
Souce Code
#include <iostream>
#include<cstring>
using namespace std;
//#define LL _int64 //这种写法是不正确的
#define LL __int64
const LL mm=1000000007;
int n,m;//n表示元素种类,m表示元素个数
LL pow[10005];//记录旋转的不动点总数
LL mod;
/* //找到大数和小数,求其公约数
LL gcd(LL a,LL b){//求最大公约数的模板,欧几里得辗转相除法
LL big=max(a,b);
LL small=min(a,b);
LL tmp=big%small;
return tmp==0 ? tmp : gcd(small,tmp);
}*/
LL gcd2(LL a, LL b)
{
return b==0 ? a : gcd2(b,a%b);
}
LL rotate(){//返回旋转时的不动点
LL ans=0;
for(int i=0;i<m;i++){
ans=(ans+pow[gcd2(i,m)])%mod;
}
return ans;
}
LL overturn(){//返回翻转时的不动点
LL ans=0;
if(m & 1) {//如果m为奇数
ans=(ans+m*pow[(m+1)/2])%mod;
}else{//如果m为偶数
ans=(ans+m/2*(pow[m/2+1]+pow[m/2]))%mod;
}
return ans;
}
int main(int argc, char** argv) {
int cas;//case是C++保留字,不能随便用
int t=0;
scanf("%d",&cas);
//while(scanf("%d",&case)==1){
while(cas--){
LL ans=0;
scanf("%d%d",&n,&m);//n表示元素种类,m表示元素个数
mod=2*m*mm;//不理解
//memset(pow,0,sizeof(pow));
//初始化pow
pow[0]=1;
for(int i=1;i<=m;i++){
pow[i]=(pow[i-1]*n)%mod;
}
ans=ans+rotate();
ans=(ans+overturn())%mod;
printf("Case #%d: %I64d\n",++t,(ans/2/m)%mm);//思考,为什么这里模mm,而不是mod呢?
}
//}
return 0;
}