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.
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 𝑦).
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 𝑛 (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.
Examples
input
4
output
2
input
5
output
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.
题意
现在有长度为n个方格需要染色,现在假设i这个格子染色了,那么所有的j满足 |j-i|>1 且 n%|j-i| == 0的格子都需要是同一个颜色。
问你最多染多少种颜色
题解
考虑循环节。
我们枚举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;
}