题解:P15798 [GESP202603 五级] 有限不循环小数
求区间内有多少个数,其倒数是有限小数。
判断一个分数是否能化为有限小数
一个有限小数,可以写成
我们设最简分数
分数
变形得:
因为
因为
而
所以
最简分数
\frac{p}{q} 能化为有限小数,当且仅当q 的质因数只包含2 和5 。
回到题目,就好做了。
从
#include <bits/stdc++.h>
using namespace std;
inline int read(){
int res = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
res = res * 10 + c - '0';
c = getchar();
}
return res * f;
}
bool check(int x){
while (x % 2 == 0) x /= 2;
while (x % 5 == 0) x /= 5;
if (x == 1) return 1;
return 0;
}
int main(){
int l = read(), r = read();
int ans = 0;
for (int i = l; i <= r; i++)
if (check(i))
ans++;
printf("%d\n", ans);
return 0;
}