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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值