Tile Painting【数学】

Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate.

The path consists of n consecutive tiles, numbered from 1 to n. Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles with numbers i and j, such that |j−i| is a divisor of n greater than 1, they have the same color. Formally, the colors of two tiles with numbers i and j should be the same if |i−j|>1 and nmod|i−j|=0 (where xmody is the remainder when dividing x by y).

Ujan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic?

Input
The first line of input contains a single integer n (1≤n≤1012), the length of the path.

Output
Output a single integer, the maximum possible number of colors that the path can be painted in.

Examples
inputCopy
4
outputCopy
2
inputCopy
5
outputCopy
5
Note
In the first sample, two colors is the maximum number. Tiles 1 and 3 should have the same color since 4mod|3−1|=0. Also, tiles 2 and 4 should have the same color since 4mod|4−2|=0.

In the second sample, all five colors can be used.
在这里插入图片描述

//package Main;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static boolean isprime(long n){
		if(n<=2) return true;
		for(int i=2;i<=Math.sqrt(n);i++){
			if(n%i==0) return false;
		}
		return true;
	}
	public static long __gcd(long a,long b){
		if(b==0) return a;
		return __gcd(b,a%b);
	}
	static int maxn = 1000010;
	static long[] pu = new long[maxn];
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long n = input.nextLong();
		if(isprime(n)) System.out.println(n);
		else{
			boolean flag=false;
			int cnt=0;
			for(int i=2;i<=Math.sqrt(n);i++){
				if(n%i==0 && (i%2==1 || (n/i)%2==1)){
					pu[++cnt]=i;
					if(i==n/i) continue;
					pu[++cnt]=(long) (n/i);
					flag=true; 	
				} 
			}
			if(flag && n%2==0) System.out.println("1");
			else if(n%2==0 && !flag) System.out.println("2");
			else{
				long gd=pu[1];
				for(int i=1;i<=cnt;i++){
					gd=__gcd(gd,pu[i]);
				}
				System.out.println(gd);
			} 
		}
	}
}

发布了444 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zt2650693774/article/details/103027332