【质因子】CF834 div2 D. Make It Round
Problem - D - Codeforces
题意:
思路:
首先是个常识:一个数末尾0的个数取决于{2,5}这一对的数量
那么考虑统计n的2和5的质因子数量,然后贪心
先把5这个质因子搞饱和了,因为*2比较小
然后把2这个质因子搞饱和
最后再*10到不能乘为止
因为要最接近m,因此最后还得乘m/d
Code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
using i64 = long long;
const int mxn=1e2+10;
int N,n,m;
void solve(){
cin>>n>>m;
N=n;
int cnt2=0,cnt5=0;
while(n%2==0){
n/=2;
cnt2++;
}
while(n%5==0){
n/=5;
cnt5++;
}
//cout<<cnt2<<" "<<cnt5<<'\n';
int d=1ll;
while(cnt2<cnt5&&d*2<=m) d*=2,cnt2++;
while(cnt5<cnt2&&d*5<=m) d*=5,cnt5++;
while(d*10<=m) d*=10;
int t=m/d;
d*=t;
cout<<N*d<<'\n';
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int __=1;cin>>__;
while(__--)solve();return 0;
}
还没有评论,来说两句吧...