Codeforces Round #599 (Div. 1) A. Tile Painting 數論

C. 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.c++

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

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

Input

The first line of input contains a single integer 𝑛 (1≤𝑛≤1012), the length of the path.翻譯

Output

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

Examples

input 4 output 2 input 5 output 5orm

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

In the second sample, all five colors can be used.rem

題意

如今有長度爲n個方格須要染色,如今假設i這個格子染色了,那麼全部的j知足 |j-i|>1 且 n%|j-i| == 0的格子都須要是同一個顏色。input

問你最多染多少種顏色it

題解

考慮循環節。

咱們枚舉n的全部的因子 a[1],a[2],a[3]....a[x]。翻譯過來就是咱們每a[1]個,都得相同;每a[2]個都得相同;....;每a[x]個都得相同。

那麼實際上這個東西的循環節就等於他們的最小公倍數。那麼最多個顏色就是n/lcm,實際上就是gcd。由於gcd x lcm = n

代碼

#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a,long long b){
	return b==0?a:gcd(b,a%b);
}
int main(){
	long long n;cin>>n;
	int flag = 0;
	long long num = n;
	for(long long i=2;i*i<=n;i++){
		if(n%i==0){
			num=gcd(num,i);
			num=gcd(num,n/i);
		}
	}
	cout<<num<<endl;
}
相關文章
相關標籤/搜索