Java implementation of Blue Bridge Cup algorithm training Rotatable Number (violent)

Test algorithm training Rotatable Number

Resource Limit
Time Limit: 1.0s Memory Limit: 256.0MB
Problem Description
  Bike is a smart kid who likes math very much. He invented the "rotatable number", which was inspired by 142857.
  As you can see, 142857 is a very magical number, because all the numbers derived from it by rotation are multiplied by 1,2,3 ..., 6 (length from 1 to number). Rotating a number means putting the last digit of it to the front. For example, by rotating 12345 you can get these numbers: 12345, 51234, 45123, 34512, 23451. It is worth mentioning that leading zeros are allowed here. Therefore, 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 meets the conditions. The following six equations all hold in decimal:
  142857 * 1 = 142857;
  142857 * 2 = 285714;
  142857 * 3 = 428571;
  142857 * 4 = 571428;
  142857 * 5 = 714285;
  142857 * 6 = 857142
  Now, Bike proposed There was a problem. He extended the "rotatable number" to an arbitrary base b. As shown above, 142857 is a "rotatable number" in decimal. Another example is 0011 in binary. The following four equations all hold in binary:
  0011 * 1 = 0011;
  0011 * 10 = 0110;
  0011 * 11 = 1001;
  0011 * 100 = 1100
  He wants to find the largest b (1 <b <x), which satisfies the existence of a positive "rotatable number" of length n in b system (leading zeros are allowed).
Input format
  Only one line contains two integers n, x separated by spaces.
Output format
  An integer on a line, indicating the largest b you find. If there is no b satisfying the condition, -1 is output.
Sample input I
  6 11
Sample output I
  10
Sample input II
  5 8
Sample output II
  -1
Data size and convention
  For 20% of data, n <= 10, x <= 15
  For 50% of data, x < = 10
  For 100% of the data, 1 <= n <= 5 * 10 ^ 6, 2 <= x <= 10 ^ 9

 
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// 转自:	https://blog.csdn.net/a1439775520   
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int x = sc.nextInt();
		sc.close();
		if (n==1&&x==3) {
			System.out.println(2);
			return;
		}
		if (!isPrime(n + 1)) {
			System.out.println(-1);
			return;
		}
		for (int i = x - 1; i >1; i--) {
			if (isRoot(i, n + 1)) {
				System.out.println(i);
				return;
			}
		}
		System.out.println(-1);
	}

	private static boolean isRoot(long a, long p) {
		if (a%p==0) {
			return false;
		}
		for (int i = 1; i * i <= p - 1; i++) {
			if ((p - 1) % i == 0) {
				if (i < p - 1 && ex(a, i, p) == 1) {
					return false;
				}
				if ((p - 1) / i < p - 1 && ex(a, (p - 1) / i, p) == 1) {
					return false;
				}
			}
		}
		return true;
	}

	private static long ex(long a, long n, long p) {
		if (n == 0) {
			return 1 % p;
		}
		long res = 1;
		while (n != 0) {
			if ((n & 1) == 1) {
				res = res * a % p;
			}
			n >>= 1;
			a = a * a % p;
		}
		return res;
	}

	private static boolean isPrime(long n) {
		for (int i = 2; i * i <= n; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}

}

1794 original articles published · 30,000 likes + · 4.49 million views

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105488593