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

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

/*
poj 2888 Magic Bracelet(Polya+矩阵快速幂) 给你m个不同的珠子组成一个长度为n的项链 (个人理解),只考虑旋转的情况下总共能够成
多少种不同的项链。 而且规定有的珠子不能挨在一起 大致还是Polya计数,只是要想办法解决找出长度为i的循环节有多少个。于是乎可以考虑
矩阵快速幂(一个经典问题:解决m步从a走到b的方案数) 主要是一直TLE,知道后来把euler改成通过素数求才过- -. 而且取模过多好像也会TLE hhh-2016-04-19 22:31:42
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
//typedef long long ll;
using namespace std;
const int maxn = ;
struct Matrix
{
int ma[][];
}; int n,m;
int isprime[maxn];
int prime[maxn];
int pnum;
void get_prime()
{
pnum = ;
memset(isprime, -, sizeof(isprime));
for(int i = ; i <= maxn-; i ++)
if(isprime[i])
{
prime[pnum ++] = i;
for(int j = i * i; j <= maxn-; j += i)
isprime[j] = ;
}
} Matrix mult(Matrix ta,Matrix tb, int mod)
{
Matrix tc;
memset(tc.ma,,sizeof(tc.ma));
for(int i = ; i < m; i ++)
for(int k = ; k < m; k ++)
if(ta.ma[i][k])
{
for(int j = ; j < m; j ++)
if(tb.ma[k][j])
tc.ma[i][j] = (tc.ma[i][j] + ta.ma[i][k] * tb.ma[k][j]) % mod;
}
return tc;
} Matrix Mat_pow(Matrix ta,int n,int mod)
{
Matrix t;
memset(t.ma,,sizeof(t.ma));
for(int i = ; i < m; i++)
t.ma[i][i] = ;
while(n)
{
if(n & ) t = mult(t,ta,mod);
ta = mult(ta,ta,mod);
n >>= ;
}
return t;
} int pow_mod(int a,int n,int mod)
{
int ret = ;
a %= mod;
while(n)
{
if(n & ) ret = ret*a,ret%=mod;
a = a*a;
a%=mod;
n >>= ;
}
return ret;
}
int mod;
int euler(int cur )
{
int ans, x;
ans = x = cur;
for(int i = ; i < pnum && prime[i] * prime[i] <= cur; i++)
if(x % prime[i] == )
{
ans = ans / prime[i] * (prime[i] - );
while(x % prime[i] == )
x /= prime[i];
}
if(x > )
ans = ans / x * (x - );
return ans%mod;
} Matrix mat;
int cal(int len)
{
int ret = ;
Matrix t = Mat_pow(mat,len,mod);
for(int i = ; i < m; i++)
ret = ret + t.ma[i][i];
ret %= mod; return ret;
} int Polya(int n)
{
int ans = ;
for(int i= ; i*i <= n; i++)
{
if(n % i == )
{
if(i*i == n)
{
ans = ans+cal(i)*euler(i);
ans%=mod;
}
else
{
ans = (ans+cal(i)*euler(n/i)+cal(n/i)*euler(i));
ans%=mod;
}
}
}
ans = ans*pow_mod(n,mod-,mod);
return ans % mod;
} int main()
{
int T;
get_prime();
mod = ;
scanf("%d",&T);
while(T--)
{
int k;
scanf("%d%d%d",&n,&m,&k);
int u,v;
for(int i = ; i < m; i++)
for(int j = ; j < m; j++)
mat.ma[i][j] = ;
for(int i = ; i <= k; i++)
{
scanf("%d%d",&u,&v);
mat.ma[u-][v-] = mat.ma[v-][u-] = ;
}
printf("%d\n",Polya(n));
}
return ;
}

poj 2888 Magic Bracelet(Polya+矩阵快速幂)的更多相关文章

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

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

  2. poj 2888 Magic Bracelet <polya定理>

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

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

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

  4. poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化

    题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...

  5. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  7. D. Magic Gems(矩阵快速幂 || 无敌杜教)

    https://codeforces.com/contest/1117/problem/D 题解:有一些魔法宝石,魔法宝石可以分成m个普通宝石,每个宝石(包括魔法宝石)占用1个空间,让你求占用n个空间 ...

  8. poj 3613 Cow Relays【矩阵快速幂+Floyd】

    !:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)

    题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 ...

随机推荐

  1. github感悟

    刚学GitHub进入网页全英文的,感觉很惊讶,自己竟然要在全英文的网站上学习,虽然是英文的但并不感觉有压力,可能之前用eclipse就是全英文的现在除了惊讶,没太多的想法.然后就是我的GitHub地址 ...

  2. 70后.net老猿,尚能饭否?

    程序猿的大限 距离上一次主动找工作,快到5年了,到现在的东家,是差不多3年前猎头挖过来的,而当时东家刚刚被欧洲一家有百年历史的跨国企业集团收购,所以我也就有幸成了一名“外企员工”,但是集团保留原东家人 ...

  3. mingw打dll ,lib包命令和调用

    1,下面的命令行将这个代码编译成 dll. gcc mydll.c -shared -o mydll.dll -Wl,--out-implib,mydll.lib 其中 -shared 告诉gcc d ...

  4. JVM学习记录

    本博客是为了自己学习JVM而建立,只记录一些自己学习的经过. 最近在看<深入理解Java虚拟机>这本书,里面的内容,很是乏味,因为看不懂所以就会觉得很枯燥,觉得很枯燥看着看着就犯困,然后就 ...

  5. spring-oauth-server实践:授权方式1、2、3和授权方式4的token对象.authorities产生方式比较

    授权方式1.2.3和授权方式4的token对象.authorities产生方式不同, 前者使用user_privillege构建, 后者直接使用oauth_client_details.authort ...

  6. Spring Security 入门(1-4-1)Spring Security - 认证过程

    理解时可结合一下这位老兄的文章:http://www.importnew.com/20612.html 1.Spring Security的认证过程 1.1.登录过程 - 如果用户直接访问登录页面 用 ...

  7. 错误解决:HibernateSystemException-HHH000142: Javassist Enhancement failed

     今天做项目报了一个错误 错误的原因是: 有级联查询的时候,一对多,多对一配置时要考虑默认延迟加载的问题,需要把延迟加载关闭. 然后就能正确查询出结果了  补充知识: 延迟加载表现在:比如:我们要查询 ...

  8. 一、Python3.6+PyQt5 安装

    一.安装PyQt5 方法一:使用pip3工具直接安装 直接在命令行中输入: Python 3.x pip3 install PyQt5 pip3 install PyQt5-tools Python ...

  9. Python系列之 - 反射

    一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...

  10. 谈谈spring-boot不同包结构下,同样的类名冲突导致服务启动失败解决方案

    项目背景: 某日,有需求要在三天的时间内完成两个大项目的项目合并,因为之前两个项目的包结构和类名都很多相同,于是开始考虑使用加一级包进行隔离,类似于这种结构 但是在启动的过程中,抛出来这样的异常: C ...