小Y喜欢拆拆拆(送)

Problem Description

小Y最喜欢把东西拆开了,虽然一般都无法组装回去(= =b)
在数学中,有一种一定能组装回去的方式是分解质因数,所以小Y最喜欢分解质因数了!但是小Y不是普通人,当然不想按常人的思路“组装”一个数,于是小Y是用以下方法来“组装”:
1. 对于一个数,如果这个数是合数,那么小Y用正常人的方法进行分解质因数,执行操作2,如果这个数不是合数,那么小Y把这个数写下来,结束工作。
2.分解质因数之后,把指数展开,然后把乘号变成加号,执行操作1.
具体操作请看提示。
请问给定一个正整数,小Y最后写下来的数是多少呢?
Input

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

Output

对于每组数据,输出一个整数,表示小Y最后写在纸上的数字。若小Y会陷入死循环,请输出-1.
Sample Input

1
2
4
6
8
10
Sample Output

1
2
-1
5
5
7
Hint

1,2都不是合数,直接写出来
4=2^2=2*2
2+2=4,陷入了死循环,输出-1
6=2*3 2+3=5 写出5
8=2^3=2*2*2 2+2+2=6 6=2*3 2+3=5 写出5
10=2*5 2+5=7 写出7
就是一个模拟题,每次分解一下,还要判断一下是否出现过,用map映射一下就好了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
bool leap[40000];
vector<ll>vt;
void init()
{
    ll len = sqrt(1000000000 + 0.5);
    for(ll i = 1;i <= len;i++)leap[i] = 1;
    for(ll i = 2;i <= len;i++)
    {
        if(leap[i])
        {
            vt.push_back(i);
            for(ll j = i*i;j <= len;j += i)leap[j] = 0;
        }
    }
}
ll change(ll n)
{
    ll ans = 0;
    ll len = sqrt(n + 0.5);
    for(ll i = 0;i < vt.size()&&vt[i] <= n;i++)
    {
        if(n%vt[i] == 0)
        {
            ll temp = vt[i];
            n = n/vt[i];
            while(n%vt[i] == 0)
            {
                temp += vt[i];
                n = n/vt[i];
            }
            ans += temp;
        }
    }
    if(n != 1)
        ans += n;
    return ans;
}
bool check(ll n)
{
    ll len = vt.size();
    for(ll i = 0;i < len;i++)
    {
        if(n%vt[i] == 0&&n != vt[i])return true;
    }
    return false;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    ll n;
    init();
    while(scanf("%lld",&n) != EOF)
    {
        map<ll,ll>gather;
        int flag = 0;
        while(!gather.count(n))
        {
            gather[n] = 1;
            if(check(n))
            {
                n = change(n);
            }
            else
            {
                flag = 1;
                printf("%lld\n",n);break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值