Educational Codeforces Round 54 (Rated for Div. 2) B. Divisor Subtraction
观察易得
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;
}