题解:P17554 「WWOI R4」二五一十
P17554 题解
前置知识
首先我们需要知道一个数末位的
还有一点,在
题意理解
题目给出一个数,为其乘上
现在设有一个数为
解法
根据上文,我们只需要知道原数中因子
由于操作对答案的影响为不增加,所以多余的操作对答案没有影响。
最后,为了防止过多的操作导致溢出,我们可以使用前置知识最后一点,每一次都把操作得到的末位
AC 代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
int len(int x) {//数字计算长度
int cnt=0;
while(x) {
cnt++;
x/=10;
}
return cnt;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--) {
int n;
cin>>n;
int n2=n,n5=n;
int cnt2=0,cnt5=0;
while(n2%2==0) {//统计2因子个数
n2/=2;
cnt2++;
}
while(n5%5==0) {//统计5因子个数
n5/=5;
cnt5++;
}
for(int i=1;i<=cnt2;i++) {//操作乘5
n*=5;
n/=10;//防止溢出
}
for(int i=1;i<=cnt5;i++) {//操作乘2
n*=2;
n/=10;//防止溢出
}
cout<<len(n)<<'\n';//输出最后数的长度
}
return 0;
}