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 ...
随机推荐
- 【Hadoop 分布式部署 三:基于Hadoop 2.x 伪分布式部署进行修改配置文件】
1.规划好哪些服务运行在那个服务器上 需要配置的配置文件 2. 修改配置文件,设置服务运行机器节点 首先在 hadoop-senior 的这台主机上 进行 解压 hadoop2.5 按照 ...
- Shell: nohup守护进程化
如果想在终端会话中启动shell脚本,然后让脚本一直以后台模式运行,直到其完成,即使你退出了终端会话,可以使用nohup命令实现.感觉nohup就是将一个进程初始化为一个守护进程. nohup命令运行 ...
- 基于OpenNetVM配置环境的发包实践
参考: openNetVM 基于OpenNetVM配置环境的发包实践 注意:本文并未对OpenNetVM的服务链进行测试,而是在借助OpenNetVM脚本环境的情况下,分别对Pktgen和MoonGe ...
- 复习ing
记不住啊,我能有什么办法,只好一遍又一遍看
- AngularJS 笔记1
2017-03-23 本文更新链接: http://www.cnblogs.com/daysme/p/6606805.html 什么是 angularjs 2009年有两个外国人创建,后由谷歌收购并开 ...
- _event
EventId 事件ID 请使用大于100的ID EventName 事件的名称,用于游戏中各种提示 NoticeText 事件开始时的弹窗内容 GossipText 功能宝石等菜单内容 Z ...
- mongdb使用技巧
进入shell的方法:mongo 命令 # 使用系统服务启动 mongodb /etc/init.d/mongod # 或 service mongod start # 或 service mon ...
- leetcode 7-> Reverse Integer(32-bit signed integer)
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...
- dbm.error: need 'c' or 'n' flag to open new db
#coding=utf-8 import shelve with shelve.open("shelve.ini","w") as f: f["k1& ...
- Digits of Factorial LightOJ - 1045
题目就不再发了,大致意思就是给你一个十进制数n,算出阶乘后转换成K进制的数,你来算一下它的位数. 坑点在哪呢,就是这个数可能算阶乘的时候没放弄了,比如1000000,做过最多单算阶乘的题也就是让你算到 ...