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 ...
随机推荐
- The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)
The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...
- cmd设置环境变量
方法,仅本次生效 set path=%path%;[新路径]方法,永久生效 setx path "%path%;[新路径]"方法,永久生效 wmic ENVIRONMENT cre ...
- 《机器学习实战》之k-近邻算法(改进约会网站的配对效果)
示例背景: 我的朋友海伦一直使用在线约会网站寻找合适自己的约会对象.尽管约会网站会推荐不同的人选,但她并不是喜欢每一个人.经过一番总结,她发现曾交往过三种类型的人: (1)不喜欢的人: (2)魅力一般 ...
- python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...
- Java常见异常:Exception in thread "main" java.lang.NoClassDefFoundError
在某一路径下执行编译好的class文件出错. 异常如下: E:\liwy>java Test98 Exception in thread "main" java.lang.N ...
- C#Listview添加数据,选中最后一行,滚屏
this.listView.Items.Add(lvi); this.listView.EnsureVisible(this.listView.Items.Count - 1); this.listV ...
- Ubuntu18.04下安装MySQL
Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client ...
- 一篇很好的java异常框架讲解
https://www.cnblogs.com/itcui/p/6400499.html 其实原作者是csdn的一名博主,实在受不了csdn的广告,所以就摘录这篇
- [转]C和C++运行时库
转自csdn原文:https://blog.csdn.net/ithzhang/article/details/20160009 图片请去原文查看 在使用VC构建项目时,经常会遇到下面的链接错误: 初 ...
- 学习笔记39—笑谈FireFox标签不同步(IOS和Wiindows)
为了解决国内用户连接 全球同步服务器 困难的问题,火狐中国版推出了 全球服务 和 本地服务 两套服务. 这两套服务的账号和数据并不通用,并且只有中国版提供了切换功能,因此当你在同步过程中遇到“未知账号 ...