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 ≤ 109gcd(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 ≤ ab ≤ 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

  用 inv[i]=(mod-mod/i)*inv[mod%i]的时候要保证i<mod才可,我一直WA最后换了种求逆元的方法就好了。
   题意是给出n个珠子组成一个环,m种颜色,k种限制,问不同的涂色方法(旋转可达的算同一种),还是找不动点,等价于求
d个珠子组成一个环的所有方案(重复的也算),m很小考虑矩阵,把可达矩阵A建立起来,res=A^(d) 表示选完d+1个珠子后的矩阵,
res[i][j]即第一个珠子是i最后一个珠子是j的方案个数,我们统计所有的res[i][i]就好了,等价于把最后一个珠子看做第一个珠子了。
其实就等价于求从i出发回到i的方案个数,这么说好理解点。
  

 #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的更多相关文章

  1. poj 2888 Magic Bracelet(Polya+矩阵快速幂)

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 D ...

  2. [POJ 2888]Magic Bracelet[Polya Burnside 置换 矩阵]

    也许更好的阅读体验 \(\mathcal{Description}\) 大意:给一条长度为\(n\)的项链,有\(m\)种颜色,另有\(k\)条限制,每条限制为不允许\(x,y\)颜色连在一起.要求有 ...

  3. POJ 2888 Magic Bracelet [Polya 矩阵乘法]

    传送门 题意:竟然扯到哈利波特了.... 和上一题差不多,但颜色数很少,给出不能相邻的颜色对 可以相邻的连边建图矩阵乘法求回路个数就得到$f(i)$了.... 感觉这样的环上有限制问题挺套路的...旋 ...

  4. poj 2888 Magic Bracelet <polya定理>

    题目:http://poj.org/problem?id=2888 题意:给定n(n <= 10^9)颗珠子,组成一串项链,每颗珠子可以用m种颜色中一种来涂色,如果两种涂色方法通过旋转项链可以得 ...

  5. POJ 2888 Magic Bracelet(Burnside引理,矩阵优化)

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 3731   Accepted: 1227 D ...

  6. POJ 2888 Magic Bracelet(burnside引理+矩阵)

    题意:一个长度为n的项链,m种颜色染色每个珠子.一些限制给出有些颜色珠子不能相邻.旋转后相同视为相同.有多少种不同的项链? 思路:这题有点综合,首先,我们对于每个n的因数i,都考虑这个因数i下的不变置 ...

  7. Matrix Power Series POJ - 3233 矩阵幂次之和。

    矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...

  8. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  9. poj 3233(矩阵高速幂)

    题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...

  10. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

随机推荐

  1. 常用模块(json/pickle/shelve/XML)

    一.json模块(重点) 一种跨平台的数据格式 也属于序列化的一种方式 介绍模块之前,三个问题: 序列化是什么? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化. 反序列化又是什么? 将 ...

  2. [小问题笔记(九)] SQL语句Not IN 效率低,用 NOT EXISTS试试

    项目中遇到这么个情况: t1表 和 t2表  都是150w条数据,600M的样子,都不算大. 但是这样一句查询 ↓ select * from t1 where phone not in (selec ...

  3. 点击返回上一页 wx.navigateTo不管用了

    做跳转的时候,发现想返回上一页,但是这个上一页又是tab上的页面,返回不了怎么办呢 wx.navigateTo({ url: '../search/search', })   解决方法: wx.reL ...

  4. _itemmod_day_limit

    控制玩家每天获得的物品上限 表说明 `comment` 备注 `entry` 物品 `limitCount`获取上限

  5. nodejs项目安装ant design

    1.确保安装好nodejs $ node --version v10.4.1 2.确保npm $ npm -v 6.1.0 3.安装 $ sudo npm install antd-init -g / ...

  6. 字符串--C++系列

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是 ...

  7. 认识flask框架

    1.框架的基本认识: 特点:用Python语言实现的轻量.简洁.扩展性强. 核心:werkzeug和Jinja2 2.虚拟环境: 创建:mkvirtualenv -p python3 名称 进入:wo ...

  8. java重新开始学习

    1.从菜鸟网站开始学习.http://www.runoob.com/java/java-tutorial.html 2. String args[] 与 String[] args 还有一个就是Str ...

  9. java调用dll/so文件

    大家都知道用C++编写的程序如果用于windows使用则编译为xxx.dll文件,如果是Linux使用则编译为libxxx.so文件.下面将java调用dll/so文件的方法粘出来方便下次使用.此处使 ...

  10. feign三:覆写feign的默认配置及feign的日志

    feign三:覆写feign的默认配置及feign的日志 默认配置复写 本项目地址:http://192.168.1.103:7601 本例是通过feign调用 eureka项目中的/eureka/a ...