当前位置:网站首页>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;
}
边栏推荐
- Some problems encountered in cocos2d-x project summary
- Securerandom things | true and false random numbers
- Debezium series: parsing the default value character set
- Thread pool parameters and reasonable settings
- 2023年深圳市绿色低碳产业扶持计划申报指南
- Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
- Ffplay document [easy to understand]
- Common operators and operator priority
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- c語言oj得pe,ACM入門之OJ~
猜你喜欢
![[untitled]](/img/51/c89d35c855e299b02137d676790eb6.png)
[untitled]

Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
![[hard core dry goods] which company is better in data analysis? Choose pandas or SQL](/img/70/a79c4a1724c11e208814de2d9cf553.png)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL

港股将迎“最牛十元店“,名创优品能借IPO突围?

leetcode刷题:二叉树12(二叉树的所有路径)

leetcode刷题:二叉树14(左叶子之和)

Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)

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

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

如何安全快速地从 Centos迁移到openEuler
随机推荐
C langue OJ obtenir PE, ACM démarrer OJ
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
微信小程序正则表达式提取链接
Thread pool parameters and reasonable settings
Leetcode brush question: binary tree 14 (sum of left leaves)
Leetcode brush questions: binary tree 18 (largest binary tree)
【c语言】快速排序的三种实现以及优化细节
Unity编辑器扩展 UI控件篇
2022年7月4日-2022年7月10日(ue4视频教程mysql)
-v parameter of GST launch
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Tasks in GStreamer
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Is it safe for CICC fortune to open an account online?
Redis cluster simulated message queue
图嵌入Graph embedding学习笔记
ICTCLAS用的字Lucene4.9捆绑
Common operators and operator priority
常用运算符与运算符优先级
[untitled]