poj-2888-矩阵+polya
Time Limit: 2000MS | Memory Limit: 131072K | |
Total Submissions: 6195 | Accepted: 1969 |
Description
Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which consists of n magic beads. The are m kinds of different magic beads. Each kind of beads has its unique characteristic. Stringing many beads together a beautiful circular magic bracelet will be made. As Harry Potter’s friend Hermione has pointed out, beads of certain pairs of kinds will interact with each other and explode, Harry Potter must be very careful to make sure that beads of these pairs are not stringed next to each other.
There infinite beads of each kind. How many different bracelets can Harry make if repetitions produced by rotation around the center of the bracelet are neglected? Find the answer taken modulo 9973.
Input
The first line of the input contains the number of test cases.
Each test cases starts with a line containing three integers n (1 ≤ n ≤ 109, gcd(n, 9973) = 1), m (1 ≤ m ≤ 10), k (1 ≤ k ≤ m(m − 1) ⁄ 2). The next k lines each contain two integers a and b (1 ≤ a, b ≤ m), indicating beads of kind a cannot be stringed to beads of kind b.
Output
Output the answer of each test case on a separate line.
Sample Input
4
3 2 0
3 2 1
1 2
3 2 2
1 1
1 2
3 2 3
1 1
1 2
2 2
Sample Output
4
2
1
0
Source
#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=;
LL N,M,K;
vector<LL>prime;
bool isp[];
struct matrix{
LL a[][];
matrix(){
memset(a,,sizeof(a));
}
matrix operator*(matrix &tmp){
matrix ans;
for(int i=;i<=M;++i){
for(int j=;j<=M;++j){
for(int k=;k<=M;++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;
}
void init(){
for(int i=;i<;++i){
if(!isp[i]) prime.push_back(i);
for(int j=;j<prime.size()&&prime[j]*i<;++j){
isp[i*prime[j]]=;
if(i%prime[j]==)break;
}
}
}
LL phi(int n){
LL ans=n,m=sqrt(n+0.5);
for(int i=;prime[i]<=m;++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%mod;
}
LL _qpow(LL a,LL b){
LL r=;
while(b){
if(b&) r=r*a%mod;
a=a*a%mod;
b>>=;
}
return r;
}
LL solve(int n){
matrix res=qpow(A,n);
LL ans=;
for(int i=;i<=M;++i) ans+=res.a[i][i];
return ans%mod;
}
int main()
{
int t,i,j,k,u,v;
init();
for(i=;i<;++i)U.a[i][i]=;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&N,&M,&K);
for(i=;i<=M;++i)
for(j=;j<=M;++j)A.a[i][j]=;
for(i=;i<=K;++i){
scanf("%d%d",&u,&v);
A.a[u][v]=A.a[v][u]=;
}
LL ans=;
for(i=;i*i<=N;++i){
if(N%i==){
ans=(ans+phi(N/i)*solve(i)%mod)%mod;
if(i*i!=N) ans=(ans+phi(i)*solve(N/i)%mod)%mod;
}
}
ans=ans*_qpow(N,mod-)%mod;
printf("%lld\n",ans);
}
return ;
}
poj-2888-矩阵+polya的更多相关文章
- poj 2888 Magic Bracelet(Polya+矩阵快速幂)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 4990 Accepted: 1610 D ...
- [POJ 2888]Magic Bracelet[Polya Burnside 置换 矩阵]
也许更好的阅读体验 \(\mathcal{Description}\) 大意:给一条长度为\(n\)的项链,有\(m\)种颜色,另有\(k\)条限制,每条限制为不允许\(x,y\)颜色连在一起.要求有 ...
- POJ 2888 Magic Bracelet [Polya 矩阵乘法]
传送门 题意:竟然扯到哈利波特了.... 和上一题差不多,但颜色数很少,给出不能相邻的颜色对 可以相邻的连边建图矩阵乘法求回路个数就得到$f(i)$了.... 感觉这样的环上有限制问题挺套路的...旋 ...
- poj 2888 Magic Bracelet <polya定理>
题目:http://poj.org/problem?id=2888 题意:给定n(n <= 10^9)颗珠子,组成一串项链,每颗珠子可以用m种颜色中一种来涂色,如果两种涂色方法通过旋转项链可以得 ...
- POJ 2888 Magic Bracelet(Burnside引理,矩阵优化)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 3731 Accepted: 1227 D ...
- POJ 2888 Magic Bracelet(burnside引理+矩阵)
题意:一个长度为n的项链,m种颜色染色每个珠子.一些限制给出有些颜色珠子不能相邻.旋转后相同视为相同.有多少种不同的项链? 思路:这题有点综合,首先,我们对于每个n的因数i,都考虑这个因数i下的不变置 ...
- Matrix Power Series POJ - 3233 矩阵幂次之和。
矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- poj 3233(矩阵高速幂)
题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
随机推荐
- [蓝桥] 历届试题 错误票据 (List用法,空格处理)
时间限制:1.0s 内存限制:256.0MB 问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是连续的,但ID的开始数码是随机选定的. 因为工作人 ...
- facebook api call——error
Error Codes send-api error-codes whatsapp api errors marketing-api error-reference graph-api/using-g ...
- -第3章 jQuery方法实现下拉菜单显示和隐藏
知识点 jquery 的引入方式 本地下载引入 在线引入 children 只获取子元素,不获取孙元素 show() 显示. hide() 隐藏. 完整代码 <!-- Author: XiaoW ...
- FB01与F-02的区别(转载)
FB01与F-02的区别(转载) FB01 : a) ''Post Document'' b) No doc type as SA automatically comes. c) No automat ...
- UVA1025 城市里的间谍
#include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...
- HDU 5782 Cycle(KMP+哈希)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...
- JQGrid 在页面加载时展开SubGrid
1.jqgrid在加载完成之后展开SubGrid,用得如下办法: gridComplete: function () { var rowIds = $("#list2").getD ...
- 5+ App开发打包指南
HTML5 Plus应用概述 HTML5 Plus移动App,简称5+App,是一种基于HTML.JS.CSS编写的运行于手机端的App,这种App可以通过扩展的JS API任意调用手机的原生能力,实 ...
- Python cmd中输入'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。
配置一下环境变量,找到 添加一下Scripts文件夹的路径,如:这是我的路径C:\Users\ck\AppData\Local\Programs\Python\Python36 就是你python的安 ...
- python 下载大文件
当使用requests的get下载大文件/数据时,建议使用使用stream模式. 当把get函数的stream参数设置成False时,它会立即开始下载文件并放到内存中,如果文件过大,有可能导致内存不足 ...