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 ...
随机推荐
- Template 制作模版
直接写在选项里的模板 直接在构造器里的template选项后边编写.这种写法比较直观,但是如果模板html代码太多,不建议这么写. var vm = new Vue({ el:"#app&q ...
- GPIO实验之c语言
上一章节进行实验使用的是汇编进行编程的,本次实验是使用c语言进行编写的. (1)点亮一个led灯 1)启动文件: crt.S .text .global _start _star ...
- (转载)Unity3D连接本地或局域网MySQL数据库
准备工作: 1.打开 Unity3D 安装目录,到这个路径下 Editor > Data > Mono > lib > mono > 2.0 拷贝出下图的五个动态链接库, ...
- 【转载】RabbitMQ基础知识
本文转自: https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message ...
- tomcat中配置https请求
一. 创建tomcat证书 这里使用JDK自带的keytool工具来生成证书: 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: ...
- 【OJ】 : 容斥原理计算出 1< =n < 1e9 中是2,3,5倍数的整数的数量
最近ACM时遇到个题,题意如下. 问题描述: 有个1到n的数列,数一下其中能够被 2, 的时候有 ,,,,.这5个数满足条件,所以我们应该输出 5 . 输入 多组输入到文件尾,每组输入一个 n (n ...
- 浅谈循环中setTimeout执行顺序问题
浅谈循环中setTimeout执行顺序问题 (下面有见解一二) 期望:开始输出一个0,然后每隔一秒依次输出1,2,3,4. for (var i = 0; i < 5; i++) { setTi ...
- C++中CopyFile、MoveFile的用法
1.含义 CopyFile(A, B, FALSE);表示将文件A拷贝到B,如果B已经存在则覆盖(第三参数为TRUE时表示不覆盖) MoveFile(A, B);表示将文件A移动到B 2.函数原型 C ...
- 【BZOJ】4013: [HNOI2015]实验比较
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4013 中第i 条涉及的图片对为(KXi, Xi),判断要么是KXi < Xi ,要么 ...
- hdu 6069 Counting Divisors 筛法
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...