[POJ1286&POJ2154&POJ2409]Polya定理
Polya定理
L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk))
G为置换群大小
m为颜色数量
c(pi)表示第i个置换的循环节数
如置换(123)(45)(6)其循环节数为3
-------------------------------------------------------------------------------------------
POJ1286&POJ2409
都是简单的处理串珠子的问题。
题目中隐藏着3种不同的置换类别。
1.旋转
注意不需要顺时针和逆时针分开考虑 因为顺时针旋转k位等同于逆时针旋转(n-k)位。
另外当旋转了k位时的循环节数为gcd(k,n) 这个略加脑补也可得出。
2.翻转(对称)
需要分n的奇偶分开讨论
当n为奇数时
只有一种对称,即以每颗珠子与中心的连线所在的直线为对称轴。这个时候循环节数为(n+1)/2。
当n为偶数时
有两种对称,一种同奇数时的情况,但是此时相对的两颗珠子所成的直线为同一条
所以循环节数为n/2+1,情况的数量也要减少一半。
另一种是以两颗珠子连线的中点与中心连线所在的直线为对称轴。这种情况的循环节数为n/2,情况也是n/2种。
这两道题的数据范围都很小,所以直接这样处理就可以了。
program poj1286;
var i,n:longint;
tot,sum:int64;
w:array[-..]of int64; function gcd(x,y:longint):longint;
begin
if y= then exit(x) else exit(gcd(y,x mod y));
end; begin
w[]:=;
for i:= to do w[i]:=w[i-]*;
readln(n);
while n<>- do
begin
if n= then
begin
writeln();
readln(n);
continue;
end;
tot:=;sum:=w[n];
for i:= to n- do
begin
inc(tot);
inc(sum,w[gcd(i,n)]);
end;
if odd(n) then
begin
inc(tot,n);
inc(sum,w[(n+) >> ]*n);
end else
begin
inc(tot,n >> );
inc(sum,w[n >> +]*n >> );
inc(tot,n >> );
inc(sum,w[n >> ]*n >> );
end;
writeln(sum div tot);
readln(n);
end;
end.
program poj2409;
var m,n,tot,sum:int64;
i:longint; function w(x:longint):int64;
var i:longint;
begin
w:=;
for i:= to x do w:=w*m;
end; function gcd(x,y:longint):longint;
begin
if y= then exit(x) else exit(gcd(y,x mod y));
end; begin
readln(m,n);
while (m<>)or(n<>) do
begin
tot:=;sum:=;
for i:= to n do
begin
inc(tot);
inc(sum,w(gcd(i,n)));
end;
if odd(n) then
begin
inc(tot,n);
inc(sum,w((n+) >> )*n);
end else
begin
inc(tot,n >> );
inc(sum,w(n >> )* n >> );
inc(tot,n >> );
inc(sum,w(n >> +)* n >> );
end;
writeln(sum div tot);
readln(m,n);
end;
end.
----------------------------------------------------------------------------------
POJ2154

感觉非常坑啊...
首先觉得这道题挺好的...数据范围非常大
由于用到gcd的统计所以自然而然想到了欧拉函数
然后套一下应该就出来了...
但是为什么我做了这么久...
有几个需要注意的地方
1.最后除以|G|在这里也就是n的步骤由于在取模意义下所以会出错,但是很神奇的发现快速幂的底数也都是n(大概这也是题目中长度和颜色数相同的意图吧),只要将次数-1就可以了。
2.直接这样会TLE,欧拉函数的求解还需要用欧拉线筛来优化。即用已有的质因子来求phi。过程很简单就不多提了。
值得一提的是最后一次TLE到AC的跨越仅仅是因为一个变量的类型。即ans变量改成longint就可以过了。
再一次印证了张老师几年前提到的”空间会影响时间“
另外在这道题中,正巧将前几天学的欧拉函数和欧拉线筛都运用了起来,感觉非常不错。
program poj2154;
const maxn=trunc(sqrt());
var t,test,n,tt:longint;
vis:array[-..maxn]of boolean;
p:array[-..maxn]of longint; procedure build;
var i,j:longint;
begin
fillchar(vis,sizeof(vis),true);
p[]:=;
for i:= to maxn do
begin
if vis[i] then
begin
inc(p[]);p[p[]]:=i;
end;
for j:= to p[] do
begin
if i*p[j]>maxn then break;
vis[i*p[j]]:=false;
if i mod p[j]= then break;
end;
end;
end; function phi(x:longint):longint;
var i,ans,tem:longint;
begin
ans:=x;tem:=x;
for i:= to p[] do if tem>=p[i]*p[i] then
//刚开始写成了tem>=p[i]就失去了优化的意义TAT
begin
if x mod p[i]= then ans:=ans div p[i]*(p[i]-);
//在ans是longint的情况下先乘后除会爆
while x mod p[i]= do x:=x div p[i];
end else break;
if x<> then ans:=ans div x*(x-);
//这里也涉及到运算顺序的问题
exit(ans mod tt);
end; function mul(a,b:longint):longint;
var ans,w:int64;
begin
ans:=;w:=a mod tt;
while b<> do
begin
if b and = then ans:=(ans*w) mod tt;
w:=(w*w) mod tt;
b:=b >> ;
end;
exit(ans);
end; function solve:longint;
var i:longint;
ans:int64;
begin
ans:=;
for i:= to trunc(sqrt(n)) do if n mod i= then
begin
ans:=(ans+phi(n div i)*mul(n,i-)) mod tt;
if i*i<>n then ans:=(ans+phi(i)*mul(n,n div i-)) mod tt;
end;
exit(ans);
end; begin
readln(test);
build;
for t:= to test do
begin
readln(n,tt);
writeln(solve);
end;
end.
[POJ1286&POJ2154&POJ2409]Polya定理的更多相关文章
- POJ2154 Color(Polya定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11654 Accepted: 3756 Description Bead ...
- poj2154 Color ——Polya定理
题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...
- poj2409(polya 定理模板)
题目链接:http://poj.org/problem?id=2409 题意:输入 m, n 表示有 m 种颜色,要构造一个长度为 n 的手环,旋转和对称的只算一种,问能组成多少个不同的手环. 思路: ...
- 【poj2154】Color Polya定理+欧拉函数
题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...
- 【poj2409】Let it Bead Polya定理
题目描述 用 $c$ 种颜色去染 $r$ 个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $r·c\le 32$ . 题解 Polya定理 Burnside引理 ...
- poj2409:Let it Bead(置换群 polya定理)
题目大意:长度为n的项链,要染m种颜色,可以通过旋转或翻转到达的状态视为同一种,问有多少种染色方案. 学了一波polya定理,发现很好理解啊,其实就是burnside定理的扩展. burnside定理 ...
- 【数论】【Polya定理】poj1286 Necklace of Beads
Polya定理:设G={π1,π2,π3........πn}是X={a1,a2,a3.......an}上一个置换群,用m中颜色对X中的元素进行涂色,那么不同的涂色方案数为:1/|G|*(mC(π1 ...
- POJ2409 Let it Bead(Polya定理)
Let it Bead Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6443 Accepted: 4315 Descr ...
- POJ2154 Color 【Polya定理 + 欧拉函数】
题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...
随机推荐
- luogu4238 【模板】多项式求逆
ref #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...
- find的详细使用
对我我这个出学者,这个已经算是很难了,不过今天整理了一下,感觉还可以接受. find Linux中十分重要的一个查找功能, [root@moban /]# find /tmp/ -type f -na ...
- 【Luogu P1935】[国家集训队]圈地计划
题目 最近房地产商 GDOI (Group of Dumbbells Or Idiots) 从 NOI (Nuts Old Idiots) 手中得到了一块开发土地. 据了解,这块土地是一块矩形的区域, ...
- Django admin源码剖析
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- Django数据模型--表关系(一对多)
一.一对一关系 使用方法:models.ForeignKey(要关联的模型) 举例说明:年级.教师和学生 from django.db import models class Grade(models ...
- nginx初探,下载安装配置负载均衡
上一篇我讲了正向代理和反向代理的概念,这个是为nginx做准备的前置技能,网上百度nginx可以知道nginx是什么: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/PO ...
- php+Mysql 页面登录代码
登录界面设置: <?php/** * Created by xx. * User: msi * Date: 2017/10/26 * Time: 18:12 *///session每次用之前都要 ...
- LTE中基于S1的切换
1:源eNodeB决定进行基于S1的切换.S1切换的原因可能是源eNodeB和目标eNodeB之间不存在X2连接,或者源eNodeB根据其他情况作出的判断. 2:源eNodeB向源MME发送Hando ...
- (转)Nginx配置和内核优化 实现突破十万并发
nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_affinity 00000001 00 ...
- PHP+AJAX 实现表格实时编辑
https://blog.csdn.net/qq_29627497/article/details/81365107 源码链接:https://pan.baidu.com/s/1fAinVXU-nWt ...