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. 1.sql简介

    在总结sql语句前,说点无聊的哈哈 SQL 是用于访问和处理数据库的标准的计算机语言. SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 S ...

  2. Luogu 4951 [USACO 2001 OPEN]地震

    水个博客玩. $01$分数规划. 题目要求$\frac{F - \sum_{i = 1}^{n}C_i}{T_i}$最大,设$\frac{F - \sum_{i}C_i}{T_i} \geq e$,移 ...

  3. osm2pgsql导入duplicate key error in slim mode

    This is a well known issue and one that is likely not going to be addressed any time soon. While the ...

  4. DataTable 转换成匿名集合类

    using System;using System.CodeDom.Compiler;using System.Collections.Generic;using System.Data;using ...

  5. rpmbuild spec 打包jar变小了、设置禁止压缩二进制文件Disable Binary stripping in rpmbuild

    Disable Binary stripping in rpmbuild 摘自:http://livecipher.blogspot.com/2012/06/disable-binary-stripp ...

  6. MongoDB整理笔记の索引

    MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引是凌驾于数 ...

  7. [.net 多线程]Monitor

    Monitor 类通过向单个线程授予对象锁来控制对对象的访问.对象锁提供限制访问代码块(通常称为临界区)的能力.当一个线程拥有对象的锁时,其他任何线程都不能获取该锁.还可以使用 Monitor 来确保 ...

  8. SQL计算时间差并排除周末

    SQL计算时间差并排除周末 CREATE FUNCTION DI_FN_GET_WorkDay (@begin DATETIME , @end DATETIME ) RETURNS int BEGIN ...

  9. VSCODE 针对调试C语言时一闪而过解决办法

    针对调试C语言时一闪而过解决办法 前提: 已经按照 C/C++ 已经安装 MINGW(并配置完成) 原因:  主要是因为tasks的配置没有写对 解决办法: tasks.json { // See h ...

  10. [转]解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式

    在上一个例子中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上.这篇文章将要在此基础上研究片段的擦除(discarding fragments)和前面剪裁.后面 ...