Educational Codeforces Round 54 (Rated for Div. 2) B. Divisor Subtraction
阿新 • • 發佈:2018-11-13
觀察易得
1.質數無1和自身外的因子 且只有本身既質又因 按題意直接一步減自身至零
2.若N是偶數則一直減2直到0
所有質數都是奇數 奇數減奇數易得偶數 再回到條件2 一步到位
所以操作次數不會太多
線篩打表 結合1 2 暴力模擬即可
/* Zeolim - An AC a day keeps the bug away */ //pragma GCC optimize(2) #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <cctype> #include <string> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <set> #include <sstream> #include <map> #include <ctime> #include <vector> #include <fstream> #include <list> #include <iomanip> #include <numeric> using namespace std; typedef long long ll; const int MAXN = 1e7 + 10; bool check[MAXN] = {0}; int prime[MAXN] = {0}; int pos = 0; int flag; void initprime(int len) { for(int i = 2; i < len; i++) { if(!check[i]) prime[pos++] = i; for(int j = 0; j < len && i * prime[j] < len; j++) { check[i * prime[j] ] = 1; if(i % prime[j] == 0) break; } } } bool isprime(ll x) { if(x < 2) return false; for(ll i = 2; i * i <= x; i++) if(x % i == 0) return false; return true; } int main() { //ios::sync_with_stdio(false); //cin.tie(0); cout.tie(0); //freopen("D://test.in", "r", stdin); //freopen("D://test.out", "w", stdout); //double start, stop; //start = clock(); //stop = clock(); //printf("%lf", (stop - start) / CLK_TCK); initprime(MAXN - 10); ll n, count = 0; scanf("%lld", &n); if(n > prime[pos - 1] && isprime(n) || prime[lower_bound(prime, prime + pos, n) - prime] == n) printf("1\n"); else { while(n) { if(n > prime[pos - 1] && isprime(n) || prime[lower_bound(prime, prime + pos, n) - prime] == n) { ++count; break; } else if(n % 2 == 0) { count += n / 2; break; } else { for(ll i = 0; i < pos; i++) { if(n % prime[i] == 0) { n -= prime[i], ++count; break; } } } } printf("%lld\n", count); } return 0; }