cdoj 1246 每周一题 拆拆拆~ 分解质因数

拆拆拆~

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/1246

Description

给你一个数x,你有两个操作

1.分解质因数,如果x是一个合数,那么就将x分解质因数,然后进入操作2,否则输出这个数

2.将分解质因数中的乘号变成加号,执行操作1

问你最后输入多少?

Input

多组数据,大概10000组,每组数据仅包含一个正整数n(1<=n<=10^9)

Output

对于每组数据,输出一个整数,表示最后的数字。如果无法得到最后的数字,输出-1

Sample Input

1
2
4
6
8
10

Sample Output

1
2
-1
5
5
7

HINT

 

题意

 

题解:

暴力分解质因数,然后模拟就好了

复杂度sqrt(n)*logn(大概是这个

代码:

#include<iostream>
#include<stdio.h>
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int now = n;
        int sum = 0;
        int flag = 0;
        int pre = 0;
        while(1)
        {
            pre = now;
            sum = 0;
            flag = 0;
            for(int i=2;i*i<=now;i++)
            {
                if(now%i==0)
                    flag = 1;
            }
            if(flag == 0)
            {
                flag = now;
                break;
            }
            for(int i=2;i*i<=now;i++)
            {
                while(now%i==0)
                {
                    flag = 1;
                    now/=i;
                    sum+=i;
                }
                if(now==1)
                    break;
            }
            if(now>1)
            {
                sum+=now;
                now = 1;
            }
            if(pre==sum)
            {
                flag = -1;
                break;
            }
            now = sum;
        }
        printf("%d\n",flag);
    }
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5006511.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞