Codeforces ~ 1076B ~ Divisor Subtraction(思维,暴力)

Codeforces ~ 1076B ~ Divisor Subtraction(思维,暴力)

题意

现在有一个数N,这个N执行以下程序:
1.如果N=0,结束程序
2.找到N最小的素因子d
3.N=dN-=d,执行次数+1
问执行次数。

思路

可以发现如果N是一个偶数,那么接下来的过程全都是 -2。可以发现需要很少次就可以把N变为一个偶数,所以暴力即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;
int main()
{
    scanf("%lld", &n);
    LL ans = 0;
    while (n%2 != 0)
    {
        LL p = n;
        for (LL i = 2; i*i <= n; i++)
            if (n%i == 0) { p = i; break; }
        n -= p;
        ans++;
    }
    ans += n/2;
    printf("%lld\n", ans);
    return 0;
}
/*
5
*/