Description
You are given an integer number nn. The following algorithm is applied to it:
- if n=0n=0, then end algorithm;
- find the smallest prime divisor dd of nn;
- subtract dd from nn and go to step 11.
Determine the number of subtrations the algorithm will make.
Input
The only line contains a single integer nn (2≤n≤10102≤n≤1010).
Output
Print a single integer — the number of subtractions the algorithm will make.
Sample Input
Input
5
Output
1
Input
4
Output
2
Hint
In the first example 55 is the smallest prime divisor, thus it gets subtracted right away to make a 00.
In the second example 22 is the smallest prime divisor at both steps.
注意:判断一个数是否是素数只要找到sqrt(n)就行!因为例如24有一个约数是3,那么一定有另一个约数是8,这一对数分别在sqrt(24)的两边,如果从2找到sqrt(n)还找不到约数,那么之后的数里边也不会有了!
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI acos(-1.0)
#define eps 1e-6
using namespace std;
typedef long long LL;
LL prime(LL n) /*判断一个数是否是素数*/
{
for(LL i=2; i<=sqrt(n); i++)
{
if(n%i==0)
return 0;
}
return 1;
}
LL check(LL n) /*找因子*/
{
for(LL i=2; i<=n/2; i++)
{
if(n%i==0)
return i;
}
}
int main()
{
LL n;
cin>>n;
if(n%2==0)
{
cout<<n/2;
}
else
{
if(prime(n))
{
cout<<"1"<<endl;
}
else
{
cout<<(n-check(n))/2+1;
}
}
return 0;
}