数论
Description
\[给定n, m \in [1, 1e9]\\ 找到使得res=n \cdot x末尾零的个数最多, 结果最大的x, 其中,\\ x \in [1, m] \]Solution
容易联想到经典题目, 求阶乘末尾零的数量. 只需要找到\(n\)中\(2\)和\(5\)的因子个数, 再用x里能有的\(2\)和\(5\)凑, 稍加模拟即可. 计算\(log_51e9 \sim log_21e9\)次.
Code
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, m;
cin >> n >> m;
int t = n, num2 = 0, num5 = 0;
while (t % 2 == 0) {
t /= 2;
num2 ++;
}
while (t % 5 == 0) {
t /= 5;
num5 ++;
}
// cout << num2 << ' ' << num5 << ' ';
if (num2 == num5) {
int cnt = 0;
t = 1;
while (t < m) {
t *= 10;
cnt ++;
}
if (t > m) {
t /= 10;
if (cnt > 0) {
cnt --;
}
}
int i;
for (i = 1; t * i <= m; ++i) {
}
if (t * i > m) {
i --;
}
t *= i;
cout << n * t << '\n';
}
if (num2 < num5) {
int need2 = num5 - num2;
t = 1;
while (need2 and t < m) {
t *= 2;
need2 --;
}
if (t > m) {
t /= 2;
need2 ++;
}
int cnt = 0;
while (t < m) {
t *= 10;
cnt ++;
}
if (t > m) {
t /= 10;
if (cnt > 0) {
cnt --;
}
}
int i;
for (i = 1; t * i <= m; ++i) {
}
if (t * i > m) {
i --;
}
t *= i;
cout << n * t << '\n';
}
if (num2 > num5) {
int need5 = num2 - num5;
t = 1;
while (need5 and t < m) {
t *= 5;
need5 --;
}
if (t > m) {
t /= 5;
need5 ++;
}
int cnt = 0;
while (t < m) {
t *= 10;
cnt ++;
}
if (t > m) {
t /= 10;
if (cnt > 0) {
cnt --;
}
}
int i;
for (i = 1; t * i <= m; ++i) {
}
if (t * i > m) {
i --;
}
t *= i;
cout << n * t << '\n';
}
}
signed main() {
IOS;
int T;
cin >> T;
while (T --) {
solve();
}
}
标签:cnt,cout,数论,每日,++,int,while,--,一题
From: https://www.cnblogs.com/whose-dream/p/16954523.html