2^x mod n = 1

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


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
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<cstdio>
#include<cmath>
int Powermod(int a,int b,int c)//快速幂
{
    int ans=1;
    if(a%c==0) return 0;
    a=a%c;
    while(b)
    {
        if(b&1)
            ans=ans*a%c;
        a=a*a%c;
        b>>=1;
    }
    return ans;
 
}
int main()
{
    int i,n;
    //奇数除了1一定有结果,偶数一定没结果
    while(~scanf("%d",&n))
    {
        if(n%2==0||n==1)//2^x对偶数求余结果为偶数,不为1   1的时候结果也不存在
       {printf("2^? mod %d = 1\n",n);continue;}
            for(i=1;; i++)//对于2^x mod n,当1<=i<=n 就能得到所有求余结果
            if(Powermod(2,i,n)==1)
            {
                printf("2^%d mod %d = 1\n",i,n);
                break;
            }
 
    }
}

  

posted @   JL_Zhou  阅读(553)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示