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 ...
随机推荐
- vue---day04
1. Node.js 1.1 介绍: - Node.js 是一个JavaScript运行环境,实质上是对Chrome V8引擎的封装. - Node.js 不是一个 JavaScript 框架,不同于 ...
- C++重载赋值操作符
1.C++中重载赋值操作函数应该返回什么? 类重载赋值操作符一般都是作为成员函数而存在的,那函数应该返回什么类型呢?参考内置类型的赋值操作,例如 int x,y,z; x=y=z=15; 赋值行为相当 ...
- python接口自动化: CAS系统验证,自动完成登录并获取token,遇到302请求重定向设置(requests模块 allow_redirects=False)即可
import requestsimport re import requests import re class Crm_token(object): try: username=int(input( ...
- Python学习笔记(二)一一一字典总结
创建方式:1 直接创建 newDictonary={‘key’:'value',} 2 列表转字典(dict函数) 3 基本操作:len 返回总数 dictionary[k] 返回k对应的值 ...
- python自动化之BDD框架之lettuce初识问题集
最近在学习虫师老师编写的python自动化的书.其中讲到了BDD结构lettuce入门一章. 因为是小白,按部就班地进行操作,先不谈执行操作如何,先来讲讲遇到的几个坑,和怎么解决的: 第一坑:pyth ...
- 问题 E: 完数与盈数
问题 E: 完数与盈数 时间限制: 1 Sec 内存限制: 32 MB提交: 73 解决: 69[提交][状态][讨论版][命题人:外部导入] 题目描述 一个数如果恰好等于它的各因子(该数本身除外 ...
- pandas DataFrame的修改方法
pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...
- linux备忘录-系统服务daemon
服务(daemon)及其分类 Linux中的服务被称为daemon(daemon是守护神,恶鬼的意思哦).这些daemon会常驻在内存当中,从而对我们的系统和任务等进行一些辅助性的工作.实际上,dae ...
- POJ 3565 Ants(最佳完美匹配)
Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...
- mongoDB 常用操作CRUD
1.显示所有的数据库 show dbs 2.切换数据库(如果没有数据库,即是创建数据库) use 数据库名称 3.显示所有的表 show tables 4.查看数据库里的表 show co ...