A positive number y is called magic number if for every positive integer x it satisfies that put y to the right of x, which will form a new integer z, z mod y = 0.
Input
The input has multiple cases, each case contains two positve integers m, n(1 <= m <= n <= 2^31-1), proceed to the end of file.
Output
For each case, output the total number of magic numbers between m and n(m, n inclusively).
Sample Input
1 1 1 10
Sample Output
1 4
记num(x)为x的位数
将y放在x右边组成的新书newnumber= x*pow(10,num(x))+y;
要使对所有x 有y|newnumber
因为 y|y
所以满足条件等价于 对所有x y|x*pow(10,num(x))
因为1|x,所以只要满足 y|pow(10,num(x)) 即可
容易发现 2|x,5|x
所以x=2^p *2^q;
枚举p和q 使之满足 x在输入的[m,n]并满足y|x*pow(10,num(x)) 即可。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int INF =0x3f3f3f3f; //const int maxn= ; ll st,ed; ll a[20],b[20];//a[x]:2^x ,b[x]: 2^x; int get(ll x)//得到x的位数 { ll s=10; for(int i=1;i;i++,s*=10) { if(x/s==0) { return i; } } } void pre() { a[0]=1; for(int i=1;i<=12;i++) { a[i]=a[i-1]*2; } b[0]=1; for(int i=1;i<=12;i++) { b[i]=b[i-1]*5; } } bool in(ll x) { return st<=x&&x<=ed; } bool is(ll x) { int num=get(x); ll s=1; for(int i=1;i<=num;i++) { s*=10; } return s%x==0; } int main() { pre(); while(~scanf("%d%d",&st,&ed)) { int num=get(ed); int ans=0; for(int x=0;x<=num;x++) { for(int y=0;y<=num;y++) { ll ret=a[x]*b[y]; if(!in(ret)) continue;//不在范围内 if(!is(ret) ) continue;//不符合条件 ans++; } } printf("%d\n",ans); } return 0; }