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. PHP学习笔记之continue与break

    百度中有人这样解释:break是结束整个循环体,continue是结束单个循环体.昨天看燕十八老师PHP视频,讲到break,continue时,举了一个例子,理解更容易.天龙八部中,西夏国公主选婿, ...

  2. Python基础 之 int、bool、str、列表、元组、字典

    数据类型 数据类型划分:可变数据类型     不可变数据类型 不可变数据类型:元组.bool.int (本身不可更改).str      (可哈希) 可变数据类型:列表list.字典dict   .集 ...

  3. [GO]给导入包起别名

    package main import io "fmt" //引用fmt这个包时,名字重命名为io import _ "os" //引用os这个包,但是不调用, ...

  4. Monkey测试异常信息解读

    查看包名 1.cmd 下面输入 adb locat > D:\test.txt 2.ctrl+c 停掉刚刚 1 运行的进程 3.打开test.txt文件--搜索  Displayed  对应的内 ...

  5. lda:变分的推导

    lda,latent diriclet allocation,是一个最基本的bayesian模型.本文要研究lda基于变分的推导方法.意义是重大的. 一.符号的定义 : the number of t ...

  6. [.net 多线程]Task

    C# 异步编程Task整理(一) c# .Net并行和多线程编程之Task学习记录! .NET 实现并行的几种方式(一) Dispatcher介绍 [C#学习笔记]使用C#中的Dispatcher 用 ...

  7. 三、Node.js-HelloWorld案例

    之前我们编写的JavaScript代码都是在浏览器中运行的,因此,我们可以直接在浏览器中敲代码,然后直接运行. 在Node,我们编写的JavaScript代码将不能在浏览器环境中执行了,而是在Node ...

  8. linux系统安全及应用——账号安全(基本安全措施)

    不开启桌面可以减少受攻击面 一.系统账号清理 1)非登录用户的shell改为/sbin/nologin ~] #usermod -s /sbin/nologin user1 2)锁定长期不用的账号 锁 ...

  9. CentOS7.3托管磁盘虚拟机扩容数据磁盘

    随着托管磁盘的上线,虚拟机支持的单块磁盘容量从1TB到达了4TB,客户对单块磁盘容量的需求量也会变的很大. 操作之前需要重点查看: 由于扩容磁盘的操作非同小可,一旦哪一步出现问题,就会导致分区损坏,数 ...

  10. WPF:CheckBox竖向的滑块效果

    原文:WPF:CheckBox竖向的滑块效果 之前做了一个横向的滑块效果,<WPF:CheckBox滑块效果>,其实我觉得那个不好看,今天又做了一个竖向的玩. <Style Targ ...