zoj 3622 Magic Number

Magic Number

Time Limit: 2 Seconds       Memory Limit: 32768 KB

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 zz mod y = 0.

Input

The input has multiple cases, each case contains two positve integers mn(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(mn 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值