每日一题-数论

数论

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();
	}
}

posted on 2022-12-06 10:33  Whosedream-0019  阅读(29)  评论(0)    收藏  举报

导航