https://codeforces.com/contest/1062/problem/B
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 x: multiplies nn by x (where x is an arbitrary positive integer).
- sqrt: replaces n 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≤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
20
output
10 2
input
5184
output
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.
题解:
By factorizing n we get n=p1a1p2a2…pkak (k is the number of prime factors). Because we can't get rid of those prime factors then the smallest nn is p1p2…pk. For each ai, let ui be the positive integer so that 2ui≥ai>2ui−1. Let U be max(ui). It's clear that we have to apply the "sqrt" operation at least U times, since each time we apply it, aiai is divided by 2 for all i. If for all ii, ai=2U then the answer is U, obviously. Otherwise, we need to use the operation "mul" 1 time to make all the ai equal 2U and by now the answer is U+1.
#include <iostream>
#include <stdio.h>
#include <map>
#include <math.h>
using namespace std;
int n;
int a[1000000+10];
map<int,int>m;
bool prime(int x){
if(n==1) return 0;
if(n==2) return 1;
for(int i=2;i<=(int)sqrt(x);i++)
if(x%i==0) return 0;
return 1;
}
int main()
{
scanf("%d",&n);
if(prime(n)){
cout << n <<" "<< 0 << endl;
}else{
int maxl=0,flag=0,ans=1,cnt;
for(int i=2;i<n;i++){
if(n%i==0&&prime(i)){
ans*=i;
while(n%i==0){
m[i]++;
n/=i;
}
if(maxl!=m[i]&&maxl!=0)
flag=1;
maxl=max(maxl,m[i]);
}
if(prime(n)){
ans*=n;
m[n]++;
if(maxl!=m[n]&&maxl!=0)
flag=1;
maxl=max(maxl,m[n]);
break;
}
}
for(int i=0;i<=20;i++){
int p=pow(2.0,i);
if(p==maxl){
cnt=i;
if(flag)
cnt++;
break;
}
if(p<maxl&&maxl<2*p){
cnt=i+2;
break;
}
}
cout << ans <<" "<< cnt << endl;
}
// << "Hello world!" << endl;
return 0;
}