HDOJ 1395 2^x mod n = 1

简介: Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.

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

题意很简单,就不用说了吧。
这个题主要就是会超范围,在这里,可以用取余的方法解决整数范围问题。
然后暴力就可以了。
首先防范一下n为偶数和等于1的情况。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n  = sc.nextInt();

            if(n%2==0||n==1){
                System.out.println("2^? mod "+n+" = 1");
                continue;
            }
            int x=1;
            boolean is = true;
            for(int i=1;i<5000;i++){
                x=x*2;
                x=x%n;
                if(x%n==1){
                    is=false;
                    System.out.println("2^"+i+" mod "+n+" = 1");
                    break;
                }
            }

            if(is){
                System.out.println("2^? mod "+n+" = 1");
            }
        }
    }
}
目录
相关文章
|
12月前
|
C++
hdoj 4288coder & cf 85d Sum of Medians
这两个题目是一样的,大概题意是有3个操作 add x, 在集合中加入x, del x 是删除x, sum 是求出由小到大排序后所有下标mod5等于3的数的和。
30 0
|
人工智能 BI
CF1169C. Increasing by Modulo(二分)
CF1169C. Increasing by Modulo(二分)
128 0
|
Go
HDOJ 1012 u Calculate e
HDOJ 1012 u Calculate e
105 0
HDOJ 1012 u Calculate e
HDOJ 1395 2^x mod n = 1
HDOJ 1395 2^x mod n = 1
92 0
HDOJ 2035 人见人爱A^B
HDOJ 2035 人见人爱A^B
129 0
HDOJ 1019 Least Common Multiple(最小公倍数问题)
HDOJ 1019 Least Common Multiple(最小公倍数问题)
93 0
HDOJ1019Least Common Multiple
HDOJ1019Least Common Multiple
93 0
|
Web App开发 Java 数据安全/隐私保护
HDOJ(HDU) 1563 Find your present!(异或)
HDOJ(HDU) 1563 Find your present!(异或)
233 0
HDOJ(HDU) 2162 Add ‘em(求和)
HDOJ(HDU) 2162 Add ‘em(求和)
69 0
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
120 0