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;
    }
    
  • 相关阅读:
    创建web应用程序时出现 SharePoint HRESULT:0x80070094 问题
    用Javascript获取SharePoint当前登录用户的用户名及Group信息
    javascript连接数据库
    sharepoint 中banner 图片的放大
    GridView导出Excel 类库
    SQL Server 性能调优
    GridView长字段的显示
    MOSS母版页制作学习笔记(二)
    sharepoint 中批量导入导出
    JavaScript 动态更改sharepoint 列表的颜色
  • 原文地址:https://www.cnblogs.com/BeautifulWater/p/15010336.html
Copyright © 2011-2022 走看看