C. Tile Painting (定理:任意一个合数都能够写成两个质数的乘积) 《Codeforces Round #599 (Div. 2) 》
Posted __MEET
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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 |j−i||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 |i−j|>1|i−j|>1 and nmod|i−j|=0nmod|i−j|=0 (where xmodyxmody is the remainder when dividing xx by yy).
Ujan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic?
The first line of input contains a single integer nn (1≤n≤10121≤n≤1012), the length of the path.
Output a single integer, the maximum possible number of colors that the path can be painted in.
4
2
5
5
In the first sample, two colors is the maximum number. Tiles 11 and 33 should have the same color since 4mod|3−1|=04mod|3−1|=0. Also, tiles 22and 44 should have the same color since 4mod|4−2|=04mod|4−2|=0.
In the second sample, all five colors can be used.
优化:判断是都是素数,如果是直接输出 N 就可以了;反之,如果所有大于2的因子的公约数不为 1 ,输出这个公约数,反之,输出 1。
分析:给定N个连续方块,假如第 k 个格子已经涂色,那么满足 N%abs(i-k)==0 && abs(i-k)>1的也要图和第k个格子一样的颜色,最多能图多少不同的颜色
Code:任意一个合数都能够写成两个质数的乘积
#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; }
以上是关于C. Tile Painting (定理:任意一个合数都能够写成两个质数的乘积) 《Codeforces Round #599 (Div. 2) 》的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #599 (Div. 2) Tile Painting