B. Math
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer nn, you can perform the following operations zero or more times:
- mul xx: multiplies nn by xx (where xx is an arbitrary positive integer).
- sqrt: replaces nn with n−−√n (to apply this operation, n−−√n must be an integer).
You can perform these operations as many times as you like. What is the minimum value of nn, that can be achieved and what is the minimum number of operations, to achieve that minimum value?
Apparently, no one in the class knows the answer to this problem, maybe you can help them?
Input
The only line of the input contains a single integer nn (1≤n≤1061≤n≤106) — the initial number.
Output
Print two integers: the minimum integer nn that can be achieved using the described operations and the minimum number of operations required.
Examples
input
Copy
20
output
Copy
10 2
input
Copy
5184
output
Copy
6 4
Note
In the first example, you can apply the operation mul 55 to get 100100 and then sqrt to get 1010.
In the second example, you can first apply sqrt to get 7272, then mul 1818 to get 12961296 and finally two more sqrt and you get 66.
Note, that even if the initial value of nn is less or equal 106106, it can still become greater than 106106 after applying one or more operations.
一、原题地址
二、大致题意
给你一个数n,每次有两种操作;
1、将数乘以 x。
2、将数开根号,且开出来的数必须是整数。
询问能将数处理为最小是多少,且输出最少需要的操作数。
三、大致思路
如果开根号是整数,那么这个数的质数的指数次数应该得是2的次方。
那么将n分解质因数后,找出质因数里最大的一个指数,然后看需要被开几次。如果他不满足2的次方,那么就需要多一次乘,不需要知道是乘多少,让它自己补去。如果正好满足2的次方,还需要查一下指数大小是否有和最大数不一样的,如果有还需要加上乘的操作。
可能直接看代码更容易理解。
四、代码
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<functional>
using namespace std;
typedef long long LL;
int n;
int num[1000005];
int bir[30];
int main()
{
for(int i=1;i<=29;i++)
{
bir[i]=(1<<i);
}
scanf("%lld",&n);
int tn=n;
int maxx=0;
int ans=1;
int anscnt=0;
set<int>ss;
while(n!=1)
{
for(int i=2;i<=n;i++)
{
if(n%i==0)
{
while(n%i==0)
{
n/=i;num[i]++;
maxx=max(maxx,num[i]);
}
ans*=i;
ss.insert(i);
}
}
}
if(maxx==1||tn==1)
{
printf("%d 0\n",ans);
return 0;
}
for(int i=1;i<=29;i++)
{
if(bir[i]>maxx)
{
anscnt=i+1;
break;
}
if(bir[i]==maxx)
{
anscnt=i;
for(set<int >::iterator it=ss.begin();it!=ss.end();it++)
{
int x=*it;
if(num[x]!=maxx)
{
anscnt++;
break;
}
}
break;
}
}
printf("%d %d\n",ans,anscnt);
return 0;
}