zoukankan      html  css  js  c++  java
  • CF 336494 B. Divisor Subtraction

    B. Divisor Subtraction

    You are given an integer number n. The following algorithm is applied to it:

    1. if n=0, then end algorithm;
    2. find the smallest prime divisor d of n;
    3. subtract d from n and go to step 1.

    Determine the number of subtrations the algorithm will make.

    Input

    The only line contains a single integer n (2≤n≤10,2≤n≤10).

    Output

    Print a single integer — the number of subtractions the algorithm will make.

    Examples

    input

    5
    

    output

    1
    

    input

    4
    

    output

    2
    

    Note

    In the first example 5 is the smallest prime divisor, thus it gets subtracted right away to make a 0.

    In the second example 2 is the smallest prime divisor at both steps.

    题意

    将一个数n减去其最小质因数,若差为0,则终止进程,否则,重复减去最小质因数的操作。

    统计一下相减的次数。

    思路(复盘)

    1. 按照题意发现,若数为偶数,其最小质因数为2,减完后其仍然为偶数,其最小质因数仍然为2,此时将会一直减2,直到为0。
    2. 质数按奇偶性分,可将2单独归为一类,另将其他非2的归为一类。而结合思路1可知,我们需要着重考虑的是当n为奇数的情况,而当n为奇数时,其最小质因数必不为偶数,为奇数,且当奇数减去奇数质因数时,结果为偶数,从而进入到思路1的解法中。
    #include<bits/stdc++.h>
    using namespace std;
    
    long long get_prime(long long n)
    {
    	for(long long i=2;i*i<=n;i++)
    	{
    		if(n%i==0)
    		   return i;
    	}	   return n;
    }
    int main()
    {
    	long long n,cnt=0;
    	cin>>n;
        if(n%2!=0)
    	{
    	    n=n-get_prime(n);   
    	    cnt++;
    	}
    	
    	cout<<cnt+n/2;
    	return 0;
    }
    
  • 相关阅读:
    alpha版本冲刺总结
    近两天项目冲刺
    关于微软必应词典客户端 的案例分析
    第三次作业——结队编程
    hdu 1002 A + B Problem II(大数)
    ZOJ 3805 Machine(二叉树,递归)
    hdu 4704 sum(费马小定理+快速幂)
    欧拉图
    hdu 1116 Play on Words(欧拉通路)
    前50个斐波那契数
  • 原文地址:https://www.cnblogs.com/BeautifulWater/p/15010336.html
Copyright © 2011-2022 走看看