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. 36.LEN() 函数

    LEN() 函数 LEN 函数返回文本字段中值的长度. SQL LEN() 语法 SELECT LEN(column_name) FROM table_name SQL LEN() 实例 我们拥有下面 ...

  2. pandas 中的 多条件分割, list 排序

    main_comment_num_3m and avg_group_order_cnt_12m = 0.863230main_comment_score_1m and avg_group_order_ ...

  3. HDU 5293 Tree chain problem

    树状数组 + dp 设$f_i$表示以$i$为根的子树中的能选取的最大和,$sum_x$表示$\sum_{f_y}$  ($y$是$x$的一个儿子),这样子我们把所有给出的链按照两点的$lca$分组, ...

  4. zend studio中安装Emmet插件后迅速编写html的方法

    table>tr*3>th*1+td*3h1{hello}            <h1>hello</h1>a[href="xx.xxx.xxx(网址) ...

  5. 不用EL表达式---实现product页面显示

    产品页面显示 静态页面如下: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  6. javascrip总结12:逻辑运算符与等号运算符

    1 逻辑运算符 逻辑运算的结果只有true 或者 false. 1.1 与&&: 两个表达式为true的时候,结果为true. 1.2 或|| 只要有一个表达式为true,结果为tru ...

  7. LightOJ 1284 Lights inside 3D Grid (数学期望)

    题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量: 析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开 ...

  8. 编写高质量代码改善C#程序的157个建议——建议61:避免在finally内撰写无效代码

    建议61:避免在finally内撰写无效代码 在阐述建议之前,需要先提出一个问题:是否存在一种打破try-finally执行顺序的情况,答案是:不存在(除非应用程序本身因为某些很少出现的特殊情况在tr ...

  9. python3 tkinter 获取输入字符串长度

    python 3  获取输入字符长度 #-*- coding:utf-8 -*- from tkinter import * from tkinter import messagebox def ge ...

  10. 策略与计费控制规则(Policy and Charging Control Rule-PCC Rule)解析及模板样例

    内容 PCC规则定义 PCC规则运行 PCC规则模板 PCC规则定义 参考文档: 3gpp ts 23.203-be0 条款6.3 策略与计费控制规则(PCC Rule),即一系列相关信息与一系列相关 ...