[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). ...
随机推荐
- xshell、xftp免费版下载方法
第一步:进入官站 https://www.netsarang.com/ 第二步:选中Free License
- CCF-NOIP-2018 提高组(复赛) 模拟试题(四)
T1 贪吃蛇 [问题描述] 贪吃蛇是一个好玩的游戏.在本题中,你需要对这个游戏进行模拟. 这个游戏在一个 \(n\) 行 \(m\) 列的二维棋盘上进行. 我们用 \((x, y)\) 来表示第 \( ...
- Laxcus大数据分布计算演示实例
Laxcus大数据管理系统提供了基于Diffuse/Converge分布算法的计算能力.算法的具体介绍详见<Laxcus:大数据处理系统>一文.本图展示了在集群环境下的随机数产生.排序.显 ...
- Floatingip
浮动IP相关功能点: 模块 功能 描述 备注 FloatingIP 创建浮动IP 指定带宽大小创建单个/多个浮动IP 指定子网.指定IP创建浮动IP 绑定浮动IP,修改带宽 绑定浮动IP到指定 ...
- packstack安装ironic
KVM Centos7.3虚机 安装openstack Pike版本, 其它版本安装方法类似. packstack目前对NetworkManager 还不支持,我们修改下配置: systemctl d ...
- LeetCode 83 —— 删除排序链表中的重复元素
1. 题目 2. 解答 从前向后遍历链表,如果下一个结点的值和当前结点的值相同,则删除下一个结点,否则继续向后遍历. /** * Definition for singly-linked list. ...
- thinkPHP form表单提交参数无法获取
后台打印获取的数据为empty, 找了半天,是因为 input标签没有写name, 真是醉了!记一下,免得以后再犯错了.
- 【iOS开发】创建单例的两种方法
创建一个单例很多办法.我先列举一个苹果官方文档中的写法. [cpp] view plaincopy static AccountManager *DefaultManager = nil; + ( ...
- 用户代理UA
简介: 用户代理英文全称为User Agent,简称UA,现在被广泛用来标识浏览器客户端信息. 发展状况: User Agent在互联网早期就已经存在,那时互联网是完全基于文本的,用户直接浏览器互联网 ...
- HTML精确定位之位置参数乱炖一锅
一.前言 公司项目,需要在一个图片的右上角放置一个类似“X”的东西(其实是需要显示一个数字,就像微信一样,在右上角显示几个消息),然后需要用到html的定位,看了几个网上的例子,恍惚间看到了一个off ...