题目链接:http://poj.org/problem?id=2154

题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种不同的情况。

思路:polya 模板,不过数据比较大,需要用欧拉优化。

代码:

 #include<iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include<vector>
using namespace std; const int MAXN = 1e5 + ;
int isprime[MAXN];
int prime[MAXN];
int num, n, p; void getprime(void){
num = ;
for(int i = ; i <= MAXN; i++)if(!isprime[i]){
prime[num++] = i;
for(int j = ; j * i <= MAXN; j++){
isprime[i * j] = ;
}
}
} int euler(int x){
int res = x;
for(int i = ; i < num && prime[i]*prime[i] <= x; i++){
if(x % prime[i] == ){
res = res / prime[i] * (prime[i] - );
while(x % prime[i] == ){
x /= prime[i];
}
}
}
if(x > ) res = res / x * (x - );
return res;
} int expmod(int a, int b, int mod){
int ret = ;
a = a % mod;
while(b > ){
if(b & )ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= ;
}
return ret;
} int main(void){
int t;
getprime();
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &p);
int ans = , i;
for(i = ; i * i < n; i++)if(n % i == ){
ans = (ans + euler(i) % p * expmod(n, n / i - , p) + euler(n / i) % p * expmod(n, i - , p)) % p;; //这里的i-1代表已经除以整个置换数n了,原本是expmod(n,i),最后要除以n的,
}
if(i * i == n)
ans = (ans + euler(i) * expmod(n, i - , p)) % p;
cout << ans << endl;
}
return ;
}

poj2154(polya定理+欧拉函数)的更多相关文章

  1. 【poj2154】Color Polya定理+欧拉函数

    题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...

  2. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  3. poj2154Color polya定理+欧拉函数优化

    没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...

  4. POJ2154 Color 【Polya定理 + 欧拉函数】

    题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...

  5. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...

  6. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

  7. poj2409 & 2154 polya计数+欧拉函数优化

    这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...

  8. 【hdu-2588】GCD(容斥定理+欧拉函数+GCD()原理)

    GCD Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  9. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

随机推荐

  1. hardentools

     Hardentools是一组简单的实用程序,旨在禁用操作系统(Microsoft Windows,现在)以及主要的消费者应用程序公开的许多“功能”.这些通常为企业客户所设想的功能,对于普通用户来说通 ...

  2. sql 2012先分离迁移mdf mlf 文件到别的机器后附加 数据库成只读的修复方法

    SQL Server2008附加数据库之后显示为只读时解决方法   从本地分离的数据库文件放到远程服务器上,附加数据库出现数据库为(只读情况) 阅读了以下两篇文章: 第一篇:http://blog.c ...

  3. Python中常用模块一

    random模块 import random # 应用数学计算 print(random.random()) # 取随机小数 范围是 0-1之间 # 应用抽奖 , 彩票 print(random.ra ...

  4. Spring Cloud Zuul 2(基于配置中心的动态API网关)

    在大体了解了API Zuul 和 配置中心Config后我们来尝试完成一个基于配置中心的动态API网关 创建项目 命名为api-gateway-dynamic-route并加入config 和 Zuu ...

  5. jQuery基础,选择器

    jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“write Less, ...

  6. 283. Move Zeroes把零放在最后面

    [抄题]: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  7. WEB前端--JavaScript

    JavaScript JavaScript基础 一.JavaScript简介 JavaScript是一种嵌入到HTML文件中的描述性语言,由浏览器的解释器将其动态地处理成可执行的代码,能独立地完成与客 ...

  8. sencha表单入门例子

    来自于<sencha touch 权威指南> ------------------------------- 一.网站结构 二.index.html代码 <!DOCTYPE HTML ...

  9. tensor 维度 问题。

    tf.argmax takes two arguments: input and dimension. example: tf.argmx(arr, dimension = 1). or tf.arg ...

  10. 复习扩展方法 涉及委托,这里我使用自定义委托类型 public delegate bb MyFunc<in T,out bb> (T arg)

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...