2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20133    Accepted Submission(s): 6321

Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
 
Input
One positive integer on each line, the value of n.
 
Output
If the minimum x exists, print a line with 2^x mod n = 1.

Print 2^? mod n = 1 otherwise.

You should replace x and n with specific numbers.

 
Sample Input
2
5
 
Sample Output
2^? mod 2 = 1
2^4 mod 5 = 1
 
Author
MA, Xiao
 
题意:求满足2^x mod n = 1的最小x的值
 
直接暴力:
#include<iostream>
#include<math.h>
#define ll long long
using namespace std;
int main()
{
ll n;
while(~scanf("%lld",&n))
{
if(n==||n%==)
printf("2^? mod %d = 1\n",n );
else
{
ll s=;
for(int i=;;i++)
{
s=s*%n;
if(s==)
{
printf("2^%d mod %d = 1\n",i,n );
break;
}
} }
}
return ; }

欧拉函数:

先求欧拉函数的值phi(n),在对phi(n)进行因数分解,把phi(n)的因数存在数组e[i]里面

然后依次枚举e[i]的每一个数,同时判断这个数e[i]是否满足2e[i]%m==1,不断更新一个最小值,最后得到答案

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
LL t,e[];
LL mod;
LL euler_phi(LL n)//欧拉函数
{
LL m=sqrt(n+0.5);
LL ans=n,i;
for(i=;i<=m;i++)
{
if(n%i==)
{
ans=ans/i*(i-);
while(n%i==)n=n/i;
}
}
if(n>)ans=ans/n*(n-);
return ans;
}
void find(LL n)//找出m的所有因子
{
LL i;
e[t++]=n;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
if(i*i==n)
e[t++]=i;
else
{
e[t++]=i;
e[t++]=n/i;
}
}
}
}
LL pows(LL a,LL b)
{
LL s=;
while(b)
{
if(b&)
s=(s*a)%mod;
a=(a*a)%mod;
b=b>>;
}
return s;
}
int main()
{
LL n;
while(cin>>n)
{
if(n%==||n==)
cout<<"2^? mod "<<n<<" = 1"<<endl;
else
{
LL m,ans,i;
m=euler_phi(n);
t=;
find(m);
sort(e,e+t);
mod=n;
for(i=;i<t;i++)
{
if(pows(,e[i])==)
{
ans=e[i];
break;
}
}
cout<<"2^"<<ans<<" mod "<<n<<" = 1"<<endl;
}
}
return ;
}

hdu1395 2^x mod n = 1(欧拉函数)的更多相关文章

  1. FZU Super A^B mod C(欧拉函数降幂)

    Problem 1759 Super A^B mod C Accept: 878    Submit: 2870 Time Limit: 1000 mSec    Memory Limit : 327 ...

  2. LightOJ 1370- Bi-shoe and Phi-shoe (欧拉函数)

    题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一个幸运数字, ...

  3. 欧拉函数(汇总&例题)

    定义 欧拉函数 $\varphi(n)$表示小于等于$n$的正整数中与$n$互质的数的数目. 性质 1.积性函数(证明). 2.$\varphi(1)=1$(显然) 3.对于质数$n$,$\varph ...

  4. HDU 3501【欧拉函数拓展】

    欧拉函数 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) . 通式:φ(x)=x*(1-1/p1)(1-1/p2)(1-1/p3)*(1-1/p4)-..(1- ...

  5. 2^x mod n = 1(欧拉定理,欧拉函数,快速幂乘)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...

  7. O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求

    筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...

  8. FZU 1759 欧拉函数 降幂公式

    Description   Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...

  9. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. ssh时传递环境变量

    设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...

  2. 类操作,removeClass&addClass

    // 添加类 function addClass(node,className){                 var reg=new RegExp("\\b"+classNa ...

  3. 设计模式06: Adapter 适配器模式(结构型模式)

    Adapter 适配器模式(结构型模式) 适配(转换)的概念无处不在:电源转接头.电源适配器.水管转接头... 动机(Motivation)在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象 ...

  4. shell 字符串中定位字符位置 获取字符位置

    linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...

  5. Android 中menu在activity中的使用

    1.在res下选择new  选择Android resource directory 2.在弹出框中Resource type选择menu 3.在res下就可以看到menu文件夹 4.在menu文件夹 ...

  6. 中国城市 json

    点击查看完整代码,再点击复制即可复制代码. 三级json,省市区: [{ "value": "110000", "text": " ...

  7. Qt绘制简单的风向玫瑰图代码

    1.绘制简单的风向玫瑰图代码2.主要使用QPainter3.在子widget上绘制需要使用widget监视事件 eventfilter update();//更新界面 //镜头12 QPainter ...

  8. Vue 编程式导航,路由history模式

    import Vue from 'vue' import App from './App.vue' import Home from './components/Home.vue' import Ne ...

  9. windows windows server2003 开机自动挂盘

    windows  windows server2003 开机自动挂盘 方案一: 设置任务计划:开机启动 方案二: 将执行文件放入启动文件夹

  10. LOJ#10172. 「一本通 5.4 练习 1」涂抹果酱

    题目链接:https://loj.ac/problem/10172 题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×MN×MN×M 的矩形,它被划分成 ...