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; }