51nod 1135 求原根(板子

基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
给出1个质数P,找出P最小的原根。
Input
输入1个质数P(3 <= P <= 10^9)
Output
输出P最小的原根。
Input示例
3
Output示例
2
李陶冶  (题目提供者)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL __int64
#define PB push_back
LL mul(LL a,LL b,LL m)
{
	LL ret = 0;
	a %= m;
	while(b)
	{
		if(b & 1) ret = (ret + a) % m;
		a = (a + a) % m;
		b >>= 1;
	}
	return ret;
}
LL a_b_MOD_c(LL a,LL b,LL m)
{
	LL ret = 1;
	a %= m;
	while(b)
	{
		if(b&1) ret = mul(ret,a,m);
		a = mul(a,a,m);
		b >>= 1;
	}
	return ret;
}


vector<LL> a;
bool g_test(LL g,LL p)
{
	for(LL i=0; i<a.size(); ++i)
		if(a_b_MOD_c(g,(p-1)/a[i],p)==1)
			return 0;
	return 1;
}
LL pri_root(LL p)
{
	a.clear();
	LL tmp=p-1;
	for(LL i=2; i<=tmp/i; ++i)
		if(tmp%i==0)
		{
			a.push_back(i);
			while(tmp%i==0)
				tmp/=i;
		}
	if(tmp!=1)
		a.push_back(tmp);
	LL g=1;
	while(true)
	{
		if(g_test(g,p))
			return g;
		++g;
	}
}


int main()
{
	LL x;
	while(~scanf("%lld",&x))
		cout<<pri_root(x)<<endl;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值