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 ...
随机推荐
- 二、Django需要的知识点
1.请求(request): 客户端到服务器端. 响应(response):服务器端到客户端. HTTP/1.1 协议共定义了 8 种请求方式,分别是: OPTIONS. HEAD. GET. POS ...
- java web相对路径和绝对路径总结
java web 开发过程中很多地方涉及url路径的问题,比如jsp页面.servlet之间的跳转.其实,可以将url中的/xxx看成一级目录,然后像看待目录层级之间的关系那样去看待url路径.接下来 ...
- RelativeSource设定绑定方向
<Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...
- 9 udp广播
udp有广播 写信 tcp没有广播· 打电话 #coding=utf-8 import socket, sys dest = ('<broadcast>', 7788) # 创建udp ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 https://www.lydsy.com/JudgeOnline/problem.php?id=2002 分析: 绵羊在弹飞的路径中相当于一棵 ...
- 用ServiceStack操作使用redis的问题
最近在学习Redis,查阅网上很多资料后使用SericeStack连接redis.在nuget中下载ServiceStack.Redis,主要使用到四个dll 但是运行之后会出现一堆奇怪问题:没有实现 ...
- Kotlin的Reified类型:怎样在函数内使用这一类型(KAD 14)
作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/reified-types-kotlin/ 对于Java开发者来说,最懊恼的 ...
- 数据库学习(二) case when then else end 的使用
case函数还用来统计数据的,参考资料:https://www.cnblogs.com/qlqwjy/p/7476533.html 这里我只是整理下工作中使用的到案例: 查询语句: SELECT t. ...
- 标准H5文件头的写法
整理代码如下: <!DOCTYPE html> <!-- 声明文档语言属性 --> <!-- 中文 --> <html lang="zh-Hans& ...
- LeetCode - 3. Longest Substring Without Repeating Characters(388ms)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...