Codeforce 1076B Divisor Subtraction 2/5

题目链接:http://codeforces.com/contest/1076/problem/B

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 nn. The following algorithm is applied to it:

  1. if n=0n=0, then end algorithm;
  2. find the smallest prime divisor dd of nn;
  3. 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

5

Output

Copy

1

Input

Copy

4

Output

Copy

2

Note

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.

题意:给一个n,和一个算法,求这个算法能执行多少次。算法内容:

1. 如果n等于0,算法结束。

2. 找到n最小的素数因子d

3. n-=d,之后回到步骤1

分析:首先这个算法并不复杂,但是考虑到n的最大值是10的10次方,肯定不能直接模拟求算法执行次数,那么进一步分析该算法,易得知,n为素数时,需要输出1;n为偶数时,先减去最小的素因子2,必然还是偶数,所以算法执行次数为n/2;那么其余的情况呢?就是n为奇数且为合数时,此时需要算出n的最小素因子,这个因子是不需要判断是否为素数的,因为它必为素数,且为奇数,那么n减去这个奇数后,变成第二种情况,答案也就得到了。

代码:

#include<stdio.h>
#include<math.h>

int isprime(long long n)
{
    double n0=(double ) n;
    long long sqn=sqrt(n0);
    sqn+=2;
    for(long long i=2 ;i<=sqn ;i++)
    {
        if(n%i==0)
            return 0;
    }
    return 1;
}
long long divisor(long long n)
{
    for(long long i=2 ; i<n ;i++ )
    {
        if(n%i==0)
        {
            return i;
        }
    }
}
int main()
{
    long long n;
    while(scanf("%I64d",&n)!=EOF)
    {
        if(n%2==0)
            printf("%I64d\n",n/2);
        else
        {
            if(isprime(n))
            {
                printf("1\n");
            }
            else
            {
                n-=divisor(n);
                printf("%I64d\n",n/2+1);

            }
        }
    }


    return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-13 19:30
化身华黑&nbsp;今天询问对接人审批情况,结果被告知没HC了&nbsp;云计算&nbsp;
苦闷的柠檬精allin实习:主管面结束后hr每周保温一次,结果前几天和我说没hc了,我也化身华黑子了
点赞 评论 收藏
分享
喜欢飞来飞去的雪碧在刷代码:可以试一试字节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务