[CF776B] Sherlock and His Girlfriend - 結論
阿新 • • 發佈:2020-09-23
Description
給定 \(n\) 個數字,分別為 \(2,3,...,n+1\),要對每個數字染色,使得一個數字是另一個數字的質因子時,兩個數字的顏色不同,最小化使用的顏色數。求染色方案。
Solution
首先證明一定可以用兩種顏色染完。不妨用第一種顏色染所有的質數,用第二種顏色染所有的非質數,顯然滿足條件。
而當不存在任意兩個數使得一個數是另一個數的質因子時,可以用一種顏色染完所有的數。由於數字是 \(2,3,4,...,n+1\),這種情況會發生,當且僅當 \(n \le 2\)。
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000005; int a[N]; signed main() { //ios::sync_with_stdio(false); int n; cin>>n; if(n<=2) { cout<<1<<endl; for(int i=1;i<=n;i++) cout<<1<<" "; } else { cout<<2<<endl; for(int i=2;i<=n+1;i++) { if(a[i]==0) { cout<<1<<" "; for(int j=i+i;j<=n+1;j+=i) { a[j]=1; } } else { cout<<2<<" "; } } } system("pause"); }