URAL 1268
来源:互联网 发布:域策略禁止使用软件 编辑:程序博客网 时间:2024/05/29 12:49
题目大意:求K个素数pi对应的ni。ni满足:ni,ni^2,ni^3,...,ni^m对pi取模各不相同(i=1,2,3,...),且m最大,ni最大。
Time Limit:250MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
数据规模:1<=K<=6553,2<pi(i=1,2,3,...)<65536。
理论基础:原根的定义:首先,对于互质的两个整数a,m。必然存在:d<=m-1,使得:a^d=1(mod m),比如说:d=phi(m)。我们定义a对m的阶为所有满足a^d=1(mod m)的d中最小的一个正整数。如此一来,如果a对m的阶为phi(m),那么我们称a为m一个原根。
原根性质定理:如果a为m的原根,记它的阶为ord,那么:a,a^2,a^3,...,a^ord对m取模的值各不相同。
定理1:对于整数a,与素数m,则a,a^m对m取模的结果相同(费马小定理)。
定理2:可以证明,如果正整数(a,m)=1和正整数 d 满足a^d=1(mod m),则ord整除d。
题目分析:由题意和提供的理论基础你或许已经知道为什么,我会直接把故事背景去掉吧。因为题目给出的是素数,所以m最大是p-1,因为:n^p=n(mod p)。所以问题转化为求一个数,在满足是p的原根的情况下,n^(p-1)的值最大。所以就是小于p的最大原根咯(为小于p,因为Little Chu不会睡超过一周(p天))。那当然是要从p-1开始进行枚举了,找到第一个是原根的数。问题转换为如何判断一个数是不是原根呢?那最笨的方法肯定是根据定义看看小于p-1的正整数中有没有数d可以使:a^d=1(mod p)。这样的话时间复杂度变为了K*O(p^2),p稍微大一点,就只能TLE,等等,有一个定理还没有用。应用定理二。我们检查的时候就可以少了好多不必要的步骤了。
代码如下:
#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<ctime>using namespace std;typedef double db;#define DBG 0#define maa (1<<31)#define mii ((1<<31)-1)#define sl(c) ((int)(c).size()) //取字符串长度;#define forl(i, a, b) for(int i = (a); i < (b); ++i) //不带边界值循环,正序#define forle(i, a, b) for(int i = (a); i <= (b); ++i) //带边界值循环,正序#define forh(i, a, b) for(int i = (a); i > (b); --i) //不带边界值,逆序#define forhe(i, a, b) for(int i = (a); i >= (b); --i) //带边界值,逆序#define forlc(i, a, b) for(int i##_b = (b), i = (a); i < i##_b; ++i) //带别名的循环,用于不可改变值#define forlec(i, a, b) for(int i##_b = (b), i = (a); i <= i##_b; ++i)#define forgc(i, a, b) for(int i##_b = (b), i = (a); i > i##_b; --i)#define forgec(i, a, b) for(int i##_b = (b), i = (a); i >= i##_b; --i)#define forall(i, v ) forl(i, 0, sz(v)) //循环所有#define forallc(i, v ) forlc(i, 0, sz(v))#define forlla(i, v ) forhe(i, sz(v)-1, 0)#define forls(i, n, a, b) for(int i = a; i != b; i = n[i]) //搜表用#define rep(n) for(int repp = 0; repp < (n); ++repp)#define repc(n) for(int repp_b = (n), repp = 0; repp < repp_b; ++repp)#define rst(a, v) memset(a, v, sizeof a) //把字符v填充到a reset 重置#define cpy(a, b) memcpy(a, b, sizeof a) //copy b 的sizeof(a)个字符到a#define rstn(a, v, n) memset(a, v, (n)*sizeof((a)[0])) //把字符v填充到a[n]之前的字节#define cpyn(a, b, n) memcpy(a, b, (n)*sizeof((a)[0])) //copy b 的 n 个字符到a#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试#define dout DBG && cout << __LINE__ << ">>| "#define pr(x) #x"=" << (x) << " | "#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl#define pra(arr, a, b) if(DBG) {\ dout<<#arr"[] |" <<endl; \ forlec(i, a, b) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \ if((b-a+1)%8) puts("");\} //数列查看#define rd(type, x) type x; cin >> x //读数inline int rdi() { int d; scanf("%d", &d); return d; }inline char rdc() { scanf(" "); return getchar(); }inline string rds() { rd(string, s); return s; }inline db rddb() { db d; scanf("%lf", &d); return d; }template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }typedef unsigned int lu;int n,m,ans,tot,f[1000],T;int pow_mod(lu a,int b){ lu res=1; while(b>0) { if(b&1)res=(res*a)%n; a=(a*a)%n; b>>=1; } return (int)res;}bool check(int x){ forl(i,0,tot) { if(pow_mod((lu)x,f[i])==1)return false; } return true;}int solve(){ if(n<=3)return n-1; tot=0,ans=m=n-1; for(int i=2;i*i<=m;i++) { if(m%i==0) { if(i*i!=m) { f[tot++]=i; f[tot++]=m/i; } else f[tot++]=i; } } sort(f,&f[tot]); pra(f,0,tot-1) while(!check(ans))ans--; return ans;}int main(){ scanf("%d",&T); while(T--) { scanf("%d",&n); printf("%d\n",solve()); } return 0;}
数论未完,待续。。。
http://zh.wikipedia.org/wiki/%E5%8E%9F%E6%A0%B9
by: Jsun_moon http://blog.csdn.net/Jsun_moon
- URAL 1268
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- HDU1099————Lottery HDU(86)
- MAC机上安装ubuntu虚拟机
- HDOJ4561 连续最大积
- 全世界最短的IE判断方法
- HDU-4144
- URAL 1268
- UpdatePanel的用法详解
- Linux 用户 quote 配置 说明
- Activity,View,Window和WindowManager之间的关系
- Java小程序-计算器
- 监视闪回数据库
- HDU-4147
- 跟着BOY 学习COCOS2D-X 网络篇---强联网(采用技术 BSD SOCKET+多线程技术 +protobuf)客户端实战篇
- 浏览器是如何工作的系列:渲染树的构建