Educational Codeforces Round 54 (Rated for Div. 2) B. Divisor Subtraction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an integer number n n . The following algorithm is applied to it:
- if n=0 n=0 , then end algorithm;
- find the smallest prime divisor d d of n n ;
- subtract d d from n n and go to step 1 1 .
Determine the number of subtrations the algorithm will make.
Input
The only line contains a single integer n n (2≤n≤10 10 2≤n≤1010 ).
Output
Print a single integer — the number of subtractions the algorithm will make.
Examples
Input
Copy
5
Output
Copy
1
Input
Copy
4
Output
Copy
2
Note
In the first example 5 5 is the smallest prime divisor, thus it gets subtracted right away to make a 0 0 .
In the second example 2 2 is the smallest prime divisor at both steps.
题解:一个数减去最小的质数之后的最小质数必然是2。知道这个,这个题就很明了了。
分为两种: 一种是像5,3这种的质数,直接减自己,结果就是1。
另一种是非质数,找见第一个质数之后,用减去质数的数字/2就是还能继续减的次数然后还要加1,因为减去第一个质数的时候有一次步骤。
如 n=21 变化如下:
21—18—16—14—12—10—8—6—4—2—0
结果是10
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
ll n; cin>>n;
ll x;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
cout<<1+(n-i)/2<<endl;
return 0;
}
}
cout<<1<<endl;
return 0;
}