Codeforces Round #520 (Div. 2) B. Math
B. Math
Description:
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 n, you can perform the following operations zero or more times:
- mul x: multiplies n by x (where x 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 n, 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≤10^6,1≤n≤10^6) — the initial number.
Output:
Print two integers: the minimum integer n that can be achieved using the described operations and the minimum number of operations required.
Sample Input:
20
Sample Output:
10 2
题意:
对n可以进行开方以及乘以一个数这两种操作(无限次),求经过操作后最小的为多少。
题解:
唯一分解定理告诉我们,n可以分解成若干个质数的乘积,比如n=a1^p1*a2^p2*...*an^pn,其中a1,a2,.....,an为质数。
由于可以乘以一个任意的数,所以我们是可以改变p1,p2..pn的值的。我们假定现在已经把指数变为可多次开方的形式,那么最小的n值就是a1*a2*...*an。
现在主要问题是解决操作次数,我们设一个数t,t为2^t>=max(p1,p2,....,pn)的最小值,那么现在我们就可以进行t次开方。
最后再判断一下乘法操作就可以了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std; typedef long long LL ;
const int N = 1e6+;
int tot,n;
int u[N],prim[N],vis[N],a[N]; int main(){
scanf("%d",&n);
for(int i=;i<=sqrt(n);i++){
for(int j=i*i;j<=n;j+=i){
if(!vis[j]) vis[j]=;
}
}
for(int i=;i<=n;i++) if(!vis[i]) prim[++tot]=i;
int tmp = n,cnt=;
while(tmp>){
if(tmp%prim[cnt]==){
tmp/=prim[cnt];
a[prim[cnt]]++;
}else{
cnt++;
}
}
int maxn = ;
cnt=;
LL ans = ,f = ;
bool flag=false;
for(int i=;i<=n;i++) if(a[i]){
maxn=max(maxn,a[i]),ans*=i;
}
for(int i=;i<=n;i++){
if(a[i]&&a[i]!=maxn) flag=true ;
}
while(){
if(f>=maxn){
if(flag) break ;
if(f==maxn) flag=false;else flag=true;
break;
}
f*=;
cnt++;
}
printf("%lld %d",ans,cnt+(flag==true));
return ;
}
后来我看了一下其它人的代码,十分简洁,发现不用把素数给筛出来,具体代码可以看下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; int n,cnt,ans=;
vector <int > vec ; int main(){
scanf("%d",&n);
int maxn = ;
for(int i=;i<=n;i++){
cnt = ;
if(n%i==) ans*=i;
while(n%i==){
n/=i;
cnt++;
}
while(<<(maxn)<cnt) maxn++;
if(cnt) vec.push_back(cnt);
}
int flag = ;
for(int i=;i<vec.size();i++){
if(vec[i]!=(<<maxn)) flag=;
}
printf("%d %d",ans,flag+maxn);
return ;
}
Codeforces Round #520 (Div. 2) B. Math的更多相关文章
- Codeforces Round #520 (Div. 2) B. Math 唯一分解定理+贪心
题意:给出一个x 可以做两种操作 ①sqrt(x) 注意必须是完全平方数 ② x*=k (k为任意数) 问能达到的最小的x是多少 思路: 由题意以及 操作 应该联想到唯一分解定理 经过 ...
- Codeforces Round #520 (Div. 2) B math(素数因子的应用)
题意: 给出一个n ; 有两个操作: 1,mul A , n=n*A : 2,sqrt() , n=sqrt(n) 开更出来必须是整数 : 求出经过这些操作后得出的最小 n , 和 ...
- Codeforces Round #520 (Div. 2)
Codeforces Round #520 (Div. 2) https://codeforces.com/contest/1062 A #include<bits/stdc++.h> u ...
- CF每日一练 Codeforces Round #520 (Div. 2)
比赛过程总结:过程中有事就玩手机了,后面打的状态不是很好,A题理解错题意,表明了内心不在状态,B题想法和思路都是完全正确的,但是并没有写出来,因为自己代码能力不强,思路不是特别清晰,把代码后面写乱了, ...
- Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)
https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
- Codeforces Round #520 (Div. 2) Solution
A. A Prank Solved. 题意: 给出一串数字,每个数字的范围是$[1, 1000]$,并且这个序列是递增的,求最多擦除掉多少个数字,使得别人一看就知道缺的数字是什么. 思路: 显然,如果 ...
- Codeforces Round #520 (Div. 2) D. Fun with Integers
D. Fun with Integers 题目链接:https://codeforc.es/contest/1062/problem/D 题意: 给定一个n,对于任意2<=|a|,|b|< ...
- Codeforces Round #520 (Div. 2) C. Banh-mi
C. Banh-mi time limit per test:1 second memory limit per test:256 megabytes 题目链接:https://codeforc.es ...
- Codeforces Round #520 (Div. 2) A. A Prank
A. A Prank time limit per test 1 second memory limit per test 256 megabytes 题目链接:https://codefo ...
随机推荐
- Spring MVC怎么统一异常管理?
1. 在类上加上@ControllerAdvice注解 2. 在方法上加上@ExceptionHandler注解 @ExceptionHandler(Exception.class) @Respons ...
- 20145202 《Java程序设计》实验五实验报告
一.实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值, ...
- Git使用之二:下载远程代码到本地指定文件夹
一.前期工作: 1.准备好本地的文件夹 2.如果后期需要继续以该文件夹进行同步的,则需要配置该文件夹,方法请参考之前的 Git使用之一:创建仓储和提交文件 二.用clone(克隆方式下载) 在本地下 ...
- Altium Designer -- 精心总结
如需转载请注明出处:http://blog.csdn.NET/qq_29350001/article/details/52199356 以前是使用DXP2004来画图的,后来转行.想来已经有一年半的时 ...
- mongodb 安装使用遇到的问题记录
mongodb 常用命令: https://www.mongodb.org/downloads 官网64位下载链接 https://fastdl.mongodb.org/linux/mongodb-l ...
- jenkins安全内容配置策略
有时我们使用HTML Publisher Plugin插件时,在jenkins点开html report,会发现没有带任何的css或js样式,这是因为Jenkins 1.641 / Jenkins 1 ...
- BI领军者之一Tableau试用浅谈
下图是最新的Gartner BI Magic Quadrant,其中领军者之一的Tableau表现的异常突出,执行力象限上直接甩开其它产品一条街,前瞻性象限上略微超越了MSBI,怀着无比的好奇心,特意 ...
- 【个人训练】(POJ3279)Fliptile
最近在刷kuangbin神犇的各种套题....感觉自己好弱啊.....还是要多多训练,跟上大神的脚步.最近的这十几题都比较水,记下来这一条我比较印象深刻.也比较难的题目吧(之后应该不会再有水题写了,珍 ...
- [推荐]spring cloud 详解
http://blog.csdn.net/column/details/15197.html
- java 生成简单word(利用Itext工具),生成简单Excel,以及下载笔记
1.java 生成简单word(包含图片表格) pom中加入itext 相关依赖 <dependency> <groupId>com.lowagie</groupId&g ...