当前位置:网站首页>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;
}
边栏推荐
- Android interview classic, 2022 Android interview written examination summary
- Zero cloud new UI design
- 淺淺的談一下ThreadLocalInsecureRandom
- 函数的概念及语法
- ACM getting started Day1
- C - sequential structure
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- A solution to PHP's inability to convert strings into JSON
- leetcode刷题:二叉树18(最大二叉树)
- Is it safe to open a mobile stock account? Is it reliable?
猜你喜欢
走入并行的世界
Parler de threadlocal insecurerandom
关于BRAM IP复位的优先级
14. Users, groups, and permissions (14)
- Oui. Net Distributed Transaction and Landing Solution
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
深度学习 卷积神经网络(CNN)基础
Recommended collection, my Tencent Android interview experience sharing
Scala基础【HelloWorld代码解析,变量和标识符】
leetcode刷题:二叉树18(最大二叉树)
随机推荐
Leetcode brush questions: binary tree 18 (largest binary tree)
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
淺淺的談一下ThreadLocalInsecureRandom
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
-v parameter of GST launch
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
Go language | 01 wsl+vscode environment construction pit avoidance Guide
【obs】libobs-winrt :CreateDispatcherQueueController
[C language] merge sort
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Leetcode brush questions: binary tree 11 (balanced binary tree)
ICTCLAS word Lucene 4.9 binding
leetcode刷题:二叉树11(平衡二叉树)
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Elk distributed log analysis system deployment (Huawei cloud)
什么是pyc文件
C language OJ gets PE, OJ of ACM introduction~
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
2022年7月4日-2022年7月10日(ue4视频教程mysql)