hdu-2865-polya+dp+矩阵+euler函数
Birthday Toy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 866 Accepted Submission(s): 456
The “Toy” is in bulk and AekdyCoin has to make one by him. Let’s assume that the “Toy” has N small white beads and one Big bead .If someone want to make a “Toy”, he (or she) must always puts the Big bead in center, and then connect the other N small beads around it by using N sticks with equal length, and then the N small beads must be connected by N sticks with equal length, and it could be seen as a regular polygon. Figure 1 shows a “Toy” with 8 small white beads and one big white bead.
Now AekdyCoin has C kinds of available color, say blue, green, yellow, pink …etc. He wants to color these beads, but he thinks that must be too boring and stupid. So he colors these beads with one role: any adjacent beads couldn’t have same color. Figure 2 shows a legal situation, and Figure 3 shows an illegal situation.
It seems that the “Toy” becomes more interesting for AekdyCoin right now; however, he wants to color the big bead in center. Of course, he should follow the role above.
Now AekdyCoin begins to play with the “Toy”, he always colors the big beads and then the other small beads. He should color under the rule above. After several minutes, AekdyCoin finally makes a perfect “Toy”. Figure 4 shows a situation that is under the color rule.
AekdyCoin now want to know the different method to color the “Toy” whit at most K color. (“Toy” contains N small beads and one big bead.)
But, no, the problem is not so easy .The repetitions that are produced by rotation around the center of the circular necklace are all neglected. Figure 5 shows 8 “Toy”, they are regard as one method.
Now AekdyCoin will give you N and K, he wants you to help him calculate the number of different methods, because the number of method is so huge, so AekdyCoin just want you to tell him the remainder when divided by M.
In this problem, M = 1,000,000,007.
Every case has only two integers indicating N, K
(3<=N<=10^9, 4<=K<=10^9)
3 5
3 17
162 78923
40
19040
19469065
N个小珠子加一个大珠子,大珠子放在中间,小的围着她形成一个等分的圆形,有k种颜色,在满足任意相邻珠子都不能同色的情况下的涂色方案数是多少,通过旋转能达到的算作一种方案。
不难想到先给大珠子一个颜色,然后求k-1种颜色涂n个小珠子的方案个数,最后乘上一个k就是答案。
ans=k/n * SUM{ C(g) } ,现在的问题是求C(g) ,也就是不动点个数,在朴素的题目里就是 k^x,但这里要求相邻珠子颜色不同就不能这么做了。先考虑把置换g分解成循环的形势,如果循环个数是1的话,不动点数量应该是0。f(i)=C(i) ,ans=k/n * SUM{f(gcd(i,n) | gcd(i,n)!=1 }
很容易想到对gcd分组利用欧拉函数减少运算次数。然后就是计算f了,这个f代表的问题等价于用k-1种颜色涂一个i珠子形成圆,相邻元素不同的方案数,用dp来求 f[i]=f[i-1]*(k-3)+f[i-2]*(k-2) (i>3) ,一个表示前一个环首尾元素不同,一个表示首尾元素相同,涵盖了所有情况。
n很大,这个递推式要用矩阵幂优化。
注意答案最后要减去gcd(i,n)==1的情况。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define PI acos(-1.0)
LL mod=1e9+,N,K,F[];
vector<LL>prime;
bool isp[];
struct matrix{
LL a[][];
matrix(){
memset(a,,sizeof(a));
}
matrix operator*(matrix &tmp){
matrix ans;
for(int i=;i<;++i){
for(int j=;j<;++j){
for(int k=;k<;++k){
ans.a[i][j]+=a[i][k]*tmp.a[k][j];
}
ans.a[i][j]%=mod;
}
}
return ans;
}
}A,U;
matrix qpow(matrix A,int b){
matrix ans=U;
while(b){
if(b&) ans=ans*A;
A=A*A;
b>>=;
}
return ans;
}
LL inv(LL n){
if(n<=) return n;
else return (mod-mod/n)*inv(mod%n)%mod;
}
LL phi(LL n){
LL ans=n;
for(int i=;prime[i]<=n;++i){
if(n%prime[i]==){
ans=ans/prime[i]*(prime[i]-);
while(n%prime[i]==)n/=prime[i];
}
}
if(n>)ans=ans/n*(n-);
return ans;
}
void init(){
for(int i=;i<=;++i){
if(!isp[i]) prime.push_back(i);
for(int j=;j<prime.size()&&i*prime[j]<=;++j){
isp[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
U.a[][]=U.a[][]=;
}
LL f(LL n){
if(n<=)return F[n];
matrix X=qpow(A,n-);
return (X.a[][]*F[]%mod+X.a[][]*F[]%mod)%mod;
}
int main()
{
int t,i,j,k,d;
init();
while(scanf("%lld%lld",&N,&K)!=EOF){ A.a[][]=K-;
A.a[][]=;
A.a[][]=K-;
A.a[][]=;
F[]=K-;
F[]=(K-)*(K-)%mod;
F[]=F[]*(K-)%mod;
LL ans=;
for(i=;i*i<N;++i){
if(N%i==){
ans=(ans+phi(N/i)*f(i))%mod;
ans=(ans+phi(i)*f(N/i))%mod;
}
}
if(i*i==N){
ans=(ans+phi(i)*f(i))%mod;
}
ans=((ans-f()*phi(N))%mod+mod)%mod;
ans=ans*K%mod;
ans=ans*inv(N)%mod;
cout<<ans<<endl;
}
return ;
}
hdu-2865-polya+dp+矩阵+euler函数的更多相关文章
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 2239 polya计数 欧拉函数
这题模数是9937还不是素数,求逆元还得手动求. 项链翻转一样的算一种相当于就是一种类型的置换,那么在n长度内,对于每个i其循环节数为(i,n),但是由于n<=2^32,肯定不能直接枚举,所有考 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 5607 graph(矩阵优化+概率DP)
该题非常easy想到求概率的转移方程:用d[i][j]表示第i步,走到j点的概率. 可是该题的k高达1e9.所以依照套路.要用矩阵相乘来优化. 第一次写矩阵相乘. 大概的意思就是利用矩阵实现递推. 而 ...
- hdu 5868 Polya计数
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...
随机推荐
- Component 初识组件
component组件是Vue学习的重点.重点.重点,重要的事情说三遍.所以你必须学好Vue component.其实组件就是制作自定义的标签,这些标签在HTML中是没有的.比如:<diy> ...
- Unity3D学习笔记(二十九):AssetBundle
AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...
- 案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
如果一个数是2的阶次方数,则它的二进制数的首位一般是1,后面全为0.比如8:1000,64:1000000,如果将这个数减1后再作与&运算,则应该全为0,(x&(x-1)==0&am ...
- Docker Engine SDKs and API 的开发2
Examples using the Docker Engine SDKs and Docker API After you install Docker, you can install the G ...
- 【转载】vim 中如何替换选中行或指定几行内的文本
https://segmentfault.com/q/1010000002552573/a-1020000002552589 :'<,'>s/替换项/替换为/g 以下命令将文中所有的字符串 ...
- TortoiseGit自动记住用户名密码的方法
TortoiseGit自动记住用户名密码的方法 windows下比较比较好用的git客户端有2种: msysgit + TortoiseGit(乌龟git) GitHub for Windows gi ...
- SE91 SAP消息类型
SE91 SAP消息类型 E:Error W:Warning I :Information A :Abortion S :Success 标准 : MESSAGE ID sy-msgid TYPE ...
- 51nod 1378 夹克老爷的愤怒(树型dp+贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1378 题意: 思路:要想放得少,尽量放在叶子节点处,叶子节点处点比较多. ...
- Java 虚拟机 最易理解的 全面解析
先上一个最容易理解的类实例化的内存模型案例截图: 转载自:https://www.zybuluo.com/Yano/note/321063 周志明著的<深入理解 Java 虚拟机>的干货~ ...
- hdu 1011 Starship Troopers 树形背包dp
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...