当前位置:网站首页>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;
}
边栏推荐
- Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
- Recommended collection, my Tencent Android interview experience sharing
- 浮动元素与父级、兄弟盒子的关系
- 如何安全快速地从 Centos迁移到openEuler
- 淺淺的談一下ThreadLocalInsecureRandom
- 建立自己的网站(16)
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- What is PyC file
- 炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
- Flume series: interceptor filtering data
猜你喜欢

Elk distributed log analysis system deployment (Huawei cloud)

ACM getting started Day1

Redis cluster simulated message queue

深度学习 卷积神经网络(CNN)基础

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

JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)

Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022

深度學習 卷積神經網絡(CNN)基礎

再忙不能忘安全

如何安全快速地从 Centos迁移到openEuler
随机推荐
1: Citation;
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
挖财钱堂教育靠谱安全吗?
gst-launch的-v参数
Go language | 01 wsl+vscode environment construction pit avoidance Guide
leetcode刷题:二叉树13(相同的树)
浅浅的谈一下ThreadLocalInsecureRandom
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
【obs】libobs-winrt :CreateDispatcherQueueController
leetcode刷题:二叉树18(最大二叉树)
js方法传Long类型id值时会出现精确损失
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Build your own website (16)
leetcode刷题:二叉树10(完全二叉树的节点个数)
多分支结构
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
[C language] three implementations of quick sorting and optimization details
Is it safe for CICC fortune to open an account online?
Add data to excel small and medium-sized cases through poi
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables