Codeforces 1076B Divisor Subtraction
线性筛法,数学问题,找规律。
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.
Examples
input
Copy
5output
Copy
1input
Copy
4output
Copy
2Note
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.
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int t;
int a[100000];
bool vis[100000];
void Init() {
memset(vis,true,sizeof(vis));
t=0;
for (int i=2; i<100000; i++)
{
if (vis[i]==true)
{
a[t++] = i;
for (int j=i+i; j<100000; j+=i)
vis[j] = false;
}
}
}
int main() {
long long n,ans=0;
Init();
cin>>n;
for (int i=0; i<t; i++)
{
if (n%a[i]==0 && (n-a[i])%2==0)
{
cout<<(n-a[i])/2+1<<endl;
return 0;
}
else if (n%a[i]==0)
{
cout<<n/a[i]<<endl;
return 0;
}
}cout<<1<<endl;
return 0;
}

浙公网安备 33010602011771号