码迷,mamicode.com
首页 > 其他好文 > 详细

题解 CF1242A 【Tile Painting】

时间:2019-11-07 15:19:36      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:https   mat   分解   中国   stream   ons   type   lock   ble   

题目链接

Sotuion Tile Painting

题目大意:给定一个长为\(n\)的序列,如果两个位置\(i,j\)满足\(n \; mod \;|i - j| =0 \;and\; |i-j| \neq 1\)的话这两个位置的颜色必须相同,问最多有多少种颜色

同余,中国剩余定理


分析:

假如\(n\)是一个质数显然答案是\(n\)

假如\(n\)可以分解为\(n=pq\),其中\(p,q\)互质(也就是说可以找最小质因子\(p\)),那么答案为\(1\)

因为对于任意一对位置\(i,j\)一定存在一个\(x \in [1,n]\)使得\(x \equiv i (mod \;p)\)并且\(x \equiv j (mod\;q)\)

证明:考虑\(CRT\),显然\(lcm(p,q) =n\),设特解为\(x'\)的话有\(x \equiv x'(mod\;n)\),得证

我们跑一遍欧拉筛递归分解即可

`cpp #include <iostream> #include <vector> using namespace std; const int maxn = 1e6 + 100; typedef long long ll; int vis[maxn + 100]; vector<int> pri; inline void sieve(){ for(int i = 2;i < maxn;i++){ if(!vis[i])pri.push_back(i); for(int x : pri){ if((ll)i * x > maxn)break; vis[i * x] = 1; if(i % x == 0)break; } } } ll n,ans,tot; inline void work(ll now){ for(int d : pri){ if((ll)d * d > now)break; if(now % d == 0){ ans = min(ans,(ll)d); tot++; while(now % d == 0)now /= d; work(now); break; } } if(now != 1)ans = min(ans,now),tot++; } int main(){ sieve(); cin >> n; ans = n; work(n); if(tot >= 2)cout << 1 << ‘\n‘; else cout << ans << ‘\n‘; return 0; }

题解 CF1242A 【Tile Painting】

标签:https   mat   分解   中国   stream   ons   type   lock   ble   

原文地址:https://www.cnblogs.com/colazcy/p/11811920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!