【poj2154】 Color
http://poj.org/problem?id=2154 (题目链接)
题意
n个珠子的项链,可以染上n中颜色,项链可以旋转不能翻转,求染色方案数。
Solution
经典的公式:
\begin{aligned} ans &= \sum_{i=0}^{n-1} gcd(n,i)\\ &= \sum_{d|n} (n^{d-1}*φ(\frac{n}{d})) \end{aligned}
于是就可以求了,然而时限卡太死,只能用int,还必须枚举质数求phi。。
代码
// poj2154
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; bool vis[1000010];
int p[100010],P; int phi(int x) {
int t=x;
for (int i=1;p[i]<=sqrt(x);i++) if (x%p[i]==0) {
t=t-t/p[i];
while (x%p[i]==0) x/=p[i];
}
if (x>1) t=t-t/x;
return t%P;
}
int power(int a,int b) {
int res=1;
while (b) {
if (b&1) res=res*a%P;
b>>=1;a=a*a%P;
}
return res;
}
int main() {
for (int i=2;i<=1000000;i++) {
if (!vis[i]) p[++p[0]]=i;
for (int j=1;j<=p[0] && p[j]*i<=1000000;j++) {
vis[p[j]*i]=1;
if (i%p[j]==0) break;
}
}
int n,ans;
int T;scanf("%d",&T);
while (T--) {
scanf("%d%d",&n,&P);ans=0;
for (int i=1;i*i<=n;i++) if (n%i==0) {
ans+=power(n%P,i-1)*phi(n/i);
if (i*i!=n) ans+=power(n%P,n/i-1)*phi(i);
ans%=P;
}
printf("%d\n",ans);
}
return 0;
}
【poj2154】 Color的更多相关文章
- 【POJ2154】Color Pólya定理+欧拉函数
[POJ2154]Color 题意:求用$n$种颜色染$n$个珠子的项链的方案数.在旋转后相同的方案算作一种.答案对$P$取模. 询问次数$\le 3500$,$n\le 10^9,P\le 3000 ...
- 【poj2154】Color Polya定理+欧拉函数
题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...
- 【BZOJ2554】Color 概率神题
[BZOJ2554]Color Description 有n个球排成一列,每个球都有一个颜色,用A-Z的大写字母来表示,我们每次随机选出两个球ball1,ball2,使得后者染上前者的颜色,求期望操作 ...
- 【C#】Color颜色对照表
Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.Light ...
- 【转】color颜色十六进制编码大全
原文网址:http://blog.csdn.net/coder_pig/article/details/18262105 最近刚开始学web,发现好的颜色搭配可以让自己的网页更加美观, 中午不想做事, ...
- 【luoguP1840】 Color the Axis_NOI导刊2011提高(05)
题目描述 在一条数轴上有N个点,分别是1—N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. 输入格式 输入一行为N ...
- 【UVALive-7040F】Color
题目大意:给定一个长度为 N 的序列,现有 M 种颜色,选出一些颜色对这个序列进行染色,要求相邻元素颜色不相同.求最终序列恰好有 K 种不同颜色的染色方案数,结果对1e9+7取模. 题解:二项式反演 ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读3
Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong 在前 ...
- 【POJ 2154】 Color (置换、burnside引理)
Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...
随机推荐
- ASP.NET MVC4入门到精通系列目录汇总
序言 最近公司在招.NET程序员,我发现好多来公司面试的.NET程序员居然都没有 ASP.NET MVC项目经验,其中包括一些工作4.5年了,甚至8年10年的,许多人给我的感觉是:工作了4.5年,We ...
- 工业物联网或系统集成中应用消息队列(ActiveMQ,C#的demo)的场景全面分析
1.[连载]<C#通讯(串口和网络)框架的设计与实现> 2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 2.应用SuperIO(SIO)和开源跨平台物联网框 ...
- C#模拟HTTP Form请求上传文件
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- bootstrap表格分页
<script src="~/Scripts/jquery.min.js"></script> <script src="~/Scripts ...
- git教程
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- Java学习-序列化
参考资料: http://www.2cto.com/kf/201405/305380.html http://www.cnblogs.com/xdp-gacl/p/3777987.html 序列化 ...
- Scala override
var 变量不能在子类中重写,除非父类是抽象类 在抽象类中var变量不能赋初值 abstract class Person{ val name="" def name1=" ...
- react-native-vector-icons的简单使用,图片,按钮,标签视图,导航条
ICONS是可以直接使用图片名, 就能加载图片的三方,使用很方便, 你不需要在工程文件夹里塞各种图片, 节省很多空间,下面就来看看怎么使用吧! 1. 首先打开terminal进入到我们的工程文件夹下, ...
- android Fragments介绍
Fragments是Android3.0引入的概念,译为片段.碎片,为了解决不同屏幕分辩率的动态和灵活UI设计. Fragment表现Activity中UI的一个行为或者一部分.可以将多个fragme ...
- iOS开发常用快捷键
二. Xcode基本快捷键 新建项目 Shift + CMD + N 项目中新建文件 CMD + N 运行 CMD + R 编译 CMD + B 停止运行 CMD + . 清除缓存 Shift + C ...