標籤:
ACM ICPC 2010-2011 NEERC Moscow Subregional Contest Moscow, October 24, 2010
Problem H. Hometask
Time limit: 1 second
Memory limit: 256 megabytes
Kolya is still trying to pass a test on Numbers Theory. The lecturer is so desperate about Kolya’s
knowledge that she gives him the same task every time.
The problem is to check if N! is divisible by N^2.
Input
The first line of input contains the only integer N (1 ≤ N ≤ 10^9).
Output
Please, print to output “YES” provided that N! is divisible by N2, otherwise print “NO”.
Examples
stdin stdout
3 NO
9 YES
Note
N! = 1 · 2 · . . . · N
Source
Moscow Subregional 2010
My Solution
素數篩選
首先 N^2 被 N!整除
也就是 N 被 (N - 1)! 整除
隨意基本上 只要 N不是素數 就可以有除了 N 和 1以外的約束,
所以只要 是素數就是 NO
此外 對於 4 由於 只有 1 2 4所以頁數不滿足 N 被 (N - 1)!整除, 所以4特殊處理 NO //還好謹慎,順便打了個表, 不然4的特殊處理可能要WA了以後再發現
0! 是 1 所以 N == 1也滿足 1^1 被 1整除 YES
複雜度 O(sqrt(n))
比賽的時候代碼是隊友實現的, 所以向隊友 CS_LYJ1997 要了AC代碼
此外我們對還過了另外2題, 但那2題, 筆者沒有參與,所以就不整理上來了
#include<iostream>#include<cmath>using namespace std;int main(){ int n,i; bool f; f=false; cin>>n; for(i=2;i<=sqrt(n);i++) if (n%i==0) f=true; if (n==1) f=true; if (!f || n==4) cout<<"NO"<<endl; else cout<<"YES"<<endl; return 0;}
Thank you!
------from ProLights
Moscow Subregional 2010 Problem H. Hometask 轉化、素數篩選