题解:P15798 [GESP202603 五级] 有限不循环小数

· · 题解

思路

若 \frac{1}{a} 可化为一个有限的,不循环的小数,则 a 应该只有 2 和 5 两个质因数,所以对于一个数,只需要一直除以 2 直到无法整除,对于 5 也是同理。

代码

#include<bits/stdc++.h>
using namespace std;
int l,r,ans;
bool check(int x){//判断是不是只有2和5两个质因数
    while(x%2==0) x/=2;
    while(x%5==0) x/=5;
    return x==1;//最后如果不是1说明还有其他质因数
}
signed main(){
    cin>>l>>r;
    for(int i=l;i<=r;i++) ans+=check(i);//累计答案
    cout<<ans;
    return 0;
}