当前位置:网站首页>Acwing周赛57-数字操作-(思维+分解质因数)
Acwing周赛57-数字操作-(思维+分解质因数)
2022-06-27 19:18:00 【可爱美少女】
题意:
就是给你一个数n,你可以让n乘以任意正整数x,或者让n开根号。问你最小可以达到的数字是多少,达到这个数字最少要多少次操作。
思考:
刚开始以为就是直接暴力跑一遍,一个数让n乘以x,再开方即可。当然这是错的,乘的可能不够。实际上就是对n先分解质因数,首先要保证达到的数最小,那么就看每个质因子的次幂,让所有的次幂都成为最大次幂,然后再一步一步开方,如果当前的次幂不是2的倍数,也就是不能直接开方了,那么就乘以这个数,让他成为2的倍数,所以用个st标记,是否要乘以数,不管乘多少次,我们都可以转化成第一次全部乘完。
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 1e5+10,M = 2010;
int T,n,m,k;
int va[N];
map<int,int > mp;
signed main()
{
IOS;
cin>>n;
if(n==1)
{
cout<<1<<" "<<0;
return 0;
}
for(int i=2;i<=n/i;i++)
{
while(n%i==0)
{
mp[i]++;
n /= i;
}
}
if(n>1) mp[n]++;
int maxn = 0,minn = inf,now = 1;
for(auto t:mp)
{
now = now*t.fi;
maxn = max(maxn,t.se);
minn = min(minn,t.se);
}
int sum = 0,st = 0;
if(minn<maxn) st = 1; //如果有的质因子的次幂不够,那么就要乘数了
while(maxn>1)
{
if(maxn&1) //如果此时次幂不是2的倍数,那么乘以now,使得变成2的倍数
{
st = 1;
maxn++;
}
maxn /= 2;
sum++;
}
cout<<now<<" "<<sum+st; //答案就是质因子的乘积,次数就是开方次数和是否要乘数
return 0;
}
总结:
多多思考,想一些算法之类的,用不确定的方法肯定不一定对。
边栏推荐
- Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
- Kirin V10 installation font
- KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
- 安装gatewayworker之后启动start.php
- AI 绘画极简教程
- 展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
- GFS分布式文件系统
- Pycharm common functions - breakpoint debugging
猜你喜欢

Codeforces Round #723 (Div. 2)

ICML2022 | 可扩展深度高斯马尔可夫随机场

MySQL usage notes 1

动物养殖生产虚拟仿真教学系统|华锐互动

Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

Graduation design of police report convenience service platform based on wechat applet

White whoring red team goby & POC, how do you call white whoring?

SQL必需掌握的100个重要知识点:过滤数据

MySQL performance optimization index function, hidden, prefix, hash index usage (2)
随机推荐
Share how I take notes
GoLand永久激活
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
GoLand permanently activated
# Leetcode 821. Minimum distance of characters (simple)
VMware vSphere esxi 7.0 installation tutorial
Icml2022 | scalable depth Gaussian Markov random field
Is it safe to open an account and buy stocks on the Internet? New to stocks, no guidance
Love math experiment | phase VI - Financial anti fraud case study
SQL必需掌握的100个重要知识点:IN 操作符
ARCS模型介绍
Day8 ---- 云资讯项目介绍与创建
实际工作中用到的shell命令 - sed
事件相关电位ERP的皮层溯源分析
Leetcode 989. Integer addition in array form (simple)
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
Zhongang Mining: the largest application field of new energy or fluorite
SQL必需掌握的100个重要知识点:组合 WHERE 子句
划重点!国产电脑上安装字体小技巧
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server