C. Tile Painting (定理:任意一個合數都可以寫成兩個質數的乘積) 《Codeforces Round #599 (Div. 2) 》

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

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

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

Input

The first line of input contains a single integer nn (1n10121≤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.atom

Examples
input
Copy
4
output
Copy
2
input
Copy
5
output
Copy
5
Note

In the first sample, two colors is the maximum number. Tiles 11 and 33 should have the same color since 4mod|31|=04mod|3−1|=0. Also, tiles 22and 44 should have the same color since 4mod|42|=04mod|4−2|=0.spa

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

 

  優化:判斷是都是素數,若是是直接輸出 N 就能夠了;反之,若是全部大於2的因子的公約數不爲 1 ,輸出這個公約數,反之,輸出 1。orm

  分析:給定N個連續方塊,假如第 k 個格子已經塗色,那麼知足 N%abs(i-k)==0 && abs(i-k)>1的也要圖和第k個格子同樣的顏色,最多能圖多少不一樣的顏色xml

  Code:任意一個合數都可以寫成兩個質數的乘積blog

#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath>
using namespace std; #define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
const int mxn = 1e6+5; ll n,m,k,ans,cnt,col,i; int a[mxn],b[mxn],flag; string str,ch ; int main() { cin>>n; for(i=2;i*i<=n && i<=n;i++) { if(!(n%i)) { while(!(n%i)) n/=i; if(n>1) { cout<<1<<endl; return 0; } else { cout<<i<<endl; return 0; } } } cout<<n<<endl; return 0; }

 

  分析:給定N個連續方塊,假如第 k 個格子已經塗色,那麼知足 N%abs(i-k)==0 && abs(i-k)>1的也要圖和第k個格子同樣的顏色,最多能圖多少不一樣的顏色

  Code:遍歷N的全部因子,gcd全部因子,由 lcm = n / gcd 得出要求的答案(有點循環節得意思)。

 

#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath>
using namespace std; #define mem(s,t) memset(s,t,sizeof(s))
#define pq priority_queue
#define pb push_back
#define fi first
#define se second
#define ac return 0;
#define ll long long
#define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
#define lc now<<1
#define rc now<<1|1
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define half no[now].l+((no[now].r-no[now].l)>>1)
#define ll long long
const int mxn = 1e6+5; ll n,m,k,ans,cnt,col; int a[mxn],b[mxn],flag; string str,ch ; int main() { while(cin>>n) { ans = n ; for(ll i = 2;i*i<=n;i++) { if(!(n%i)) ans = __gcd(__gcd(ans,n/i),i); if(ans==1) break; } cout<<ans<<endl; } return 0; }
相關文章
相關標籤/搜索