当前位置:网站首页>E. Singhal and Numbers(质因数分解)
E. Singhal and Numbers(质因数分解)
2022-07-05 20:08:00 【小酒窝.】
https://codeforces.com/gym/102767/problem/E
题意
给定一个数 n,可以选择小于 n 的因数作为 N。
- 如果 N 是质数,那么答案为 A + X N A + \frac{X}{N} A+NX;
- 如果 N 是合数,那么答案为 B + X N B + \frac{X}{N} B+NX;
- 如果 N 为1,那么答案为 C + X N C + \frac{X}{N} C+NX.
问,答案最小是多少?
思路
为了使答案最小,一定要找最大的质因数,最大的合因数。
质因数分解找到最大质数因子,O( n \sqrt{n} n).
为了找到最大的合数,可以先找到最大因数:n / 最小质因数。
- 如果最大因数是质数的话,说明其没有最大合因数。
- 否则最大因数就是最大合因数。
注意此题中因数要小于 n,所以可以先特判掉 n 是质数的情况。
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a, b, c;
bool isPrim(int x){
if(x == 0 || x == 1) return 0;
for(int i=2;i<=x/i;i++)
{
if(x % i == 0) return 0;
}
return 1;
}
signed main(){
Ios;
cin >> T;
while(T--)
{
cin >> n;
cin >> a >> b >> c;
if(isPrim(n)){
cout << c + n << endl;
continue;
}
int mprim = -1, mcom = -1;
int minprim = -1;
int t = n;
for(int i=2;i<=t/i;i++)
{
if(t % i == 0)
{
while(t % i == 0) t/=i, mprim = i;
if(minprim == -1) minprim = i;
}
}
if(t != 1) mprim = t;
int min1 = 1e18, min2 = 1e18, min3 = 1e18;
min1 = a+n/mprim;
mcom = n/minprim;
if(!isPrim(mcom)) min2 = b+n/mcom;
min3 = c+n;
int ans = min(min(min1, min2), min3);
cout << ans << endl;
}
return 0;
}
边栏推荐
- Database logic processing function
- Securerandom things | true and false random numbers
- 1:引文;
- Tasks in GStreamer
- [C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
- Where is the operation of new bonds? Is it safer and more reliable to open an account
- 再忙不能忘安全
- c語言oj得pe,ACM入門之OJ~
- openh264解码数据流向分析
- 图嵌入Graph embedding学习笔记
猜你喜欢

Leetcode brush question: binary tree 14 (sum of left leaves)

Oracle-表空间管理

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

How to select the Block Editor? Impression notes verse, notation, flowus

.Net分布式事務及落地解决方案

Zero cloud new UI design

Elk distributed log analysis system deployment (Huawei cloud)

JVMRandom不可设置种子|问题追溯|源码追溯

Add data to excel small and medium-sized cases through poi

Database logic processing function
随机推荐
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
Go language learning tutorial (XV)
ffplay文档[通俗易懂]
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Go language | 01 wsl+vscode environment construction pit avoidance Guide
零道云新UI设计中
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
安信证券在网上开户安全吗?
js方法传Long类型id值时会出现精确损失
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
leetcode刷题:二叉树15(找树左下角的值)
什么是pyc文件
建立自己的网站(16)
微信小程序正则表达式提取链接
Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
Is it safe for Anxin securities to open an account online?
leetcode刷题:二叉树14(左叶子之和)
What is PyC file
走入并行的世界
leetcode刷题:二叉树10(完全二叉树的节点个数)