当前位置:网站首页>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;
}
边栏推荐
- Leetcode skimming: binary tree 12 (all paths of binary tree)
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- c——顺序结构
- Summer Challenge harmonyos - realize message notification function
- Is the education of caiqiantang reliable and safe?
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- ICTCLAS word Lucene 4.9 binding
- A solution to PHP's inability to convert strings into JSON
- Bzoj 3747 poi2015 kinoman segment tree
- Build your own website (16)
猜你喜欢
![[untitled]](/img/51/c89d35c855e299b02137d676790eb6.png)
[untitled]

如何安全快速地从 Centos迁移到openEuler

Go language | 01 wsl+vscode environment construction pit avoidance Guide

B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05

零道云新UI设计中

关于BRAM IP复位的优先级

leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)

深度学习 卷积神经网络(CNN)基础
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

Leetcode brush questions: binary tree 18 (largest binary tree)
随机推荐
Gstreamer中的task
Recommended collection, my Tencent Android interview experience sharing
leetcode刷题:二叉树11(平衡二叉树)
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Concept and syntax of function
A solution to PHP's inability to convert strings into JSON
Thread pool parameters and reasonable settings
Go language | 01 wsl+vscode environment construction pit avoidance Guide
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Where is the operation of new bonds? Is it safer and more reliable to open an account
Build your own website (16)
【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
c——顺序结构
【c语言】归并排序
JVMRandom不可设置种子|问题追溯|源码追溯
深度学习 卷积神经网络(CNN)基础
Scala基础【HelloWorld代码解析,变量和标识符】
gst-launch的-v参数
Leetcode skimming: binary tree 12 (all paths of binary tree)
浅浅的谈一下ThreadLocalInsecureRandom