2^x mod n = 1

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

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.

 
思路:
1. 当n为偶数时,bn + 1(b为整数)是奇数,而2^x是偶数,故 2^x mod n = 1不可能成立;
2. 当n等于1时,不能成立
3. 当n为非1的奇数时,n和2互质,由欧拉定理:若a,n为正整数,且两者互素,则a^phi(n) mod n = 1,其中phi(n)是n的欧拉函数。知2^phi(n) mod n = 1.因此phi(n)必是符合要求的x,但phi(n)未必是最小的,遍历小于其的正整数,逐一试验即可,计算2^x mod n时用快速幂乘。
 
AC Code:
 #include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; //计算n的欧拉函数
int Eular(int n)
{
int res = , i;
for (i = ; i * i <= n; i++){
if (n % i == ){
n /= i;
res *= (i - );
while (n % i == ){
n /= i;
res *= i;
}
}
}
if (n > ) res *= (n - );
return res;
} //快速幂乘计算2^b % n
int myPow(int b, int n)
{
if(b == ) return ;
long long c = myPow(b >> , n);
c = (c * c) % n;
if(b & ) c = ( * c) % n;
return c;
} int main()
{
int n, x;
bool ok;
while(scanf("%d", &n) != EOF){
ok = ;
if((n & ) && (n - )){
ok = ;
int phi = Eular(n);
for(x = ; x < phi; x++){
if(myPow(x, n) == ) break;
}
}
if(ok) printf("2^%d mod %d = 1\n", x, n);
else printf("2^%? mod %d = 1\n", n);
}
return ;
}

2^x mod n = 1(欧拉定理,欧拉函数,快速幂乘)的更多相关文章

  1. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

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

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

  3. 牛客训练:小a与黄金街道(欧拉函数+快速幂)

    题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...

  4. 数学知识-欧拉函数&快速幂

    欧拉函数 定义 对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n). 算法思路 既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数 因此,我们只需求出他的质因子, ...

  5. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  6. 小a与黄金街道(欧拉函数+快速幂)

    链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...

  7. hdu1395 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 ...

  8. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

  9. [LightOJ 1370] Bi-shoe and Phi-shoe(欧拉函数快速筛法)

    题目链接: https://vjudge.net/problem/LightOJ-1370 题目描述: 给出一些数字,对于每个数字找到一个欧拉函数值大于等于这个数的数,求找到的所有数的最小和. 知识点 ...

  10. 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

    读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...

随机推荐

  1. mysql join 查询图

    mysql join 查询,特别是对查两个表之间的差集,可以用table字段=null来做. 注意千万不是join on XX!=XX  ,这样出来的结果是错误的.

  2. atitit.提升开发效率---mda 软件开发方式的革命--(2)

    atitit.提升开发效率---mda 软件开发方式的革命--(2) 1. 一个完整的MDA规范包含: 1 2. 一个完整的MDA应用程序包含: 1 3. MDA能够带来的最大的三个好处是什么? 2 ...

  3. atitit. 解决org.hibernate.SessionException Session is closed

    atitit. 解决org.hibernate.SessionException Session is closed   #--现象:: org.hibernate.SessionException ...

  4. tab切换的两种方法

    方法一.主要使用了传递参数的思想,把循环变量不能使用转换了一下<!DOCTYPE html><html lang="en"><head> < ...

  5. cmd命令生成android签名证书

    cmd命令生成android签名证书,有空在写一篇eclipse导出带签名的apk,这里面包括生成新的签名.现在还是讲讲在cmd怎么操作生成签名证书. 1.dos下进入JDK的bin目录 运行如下命令 ...

  6. Abstract Server模式,Adapter模式和Bridge模式

    简易的台灯 Abstract Server模式 谁拥有接口. 接口属于它的客户,而不是它的派生类. 接口和客户之间的逻辑关系,强于接口和其派生类的逻辑关系. 逻辑关系和实体关系的强度是不一致的.在实体 ...

  7. Leetcode 13 Roman to Integer 字符串处理+STL

    题意:将罗马数字1到3999转化成自然数字,这里用了STL库map将罗马字符映射到自然数字. I,V,X,L,C,D,M -> 1,5,10,50,100,500,1000 m[s[i]]< ...

  8. Language

    TODO Java(jdk8),Scala,Spring,Spark,Python,Linux,C,Netty,HttpClient,Hadoop YARN,RaspberryPi Jetty,Tom ...

  9. libev代码

    就是贴上来: ev.c: /* * libev event processing core, watcher management */ /* this big block deduces confi ...

  10. Scala 深入浅出实战经典 第63讲:Scala中隐式类代码实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...