Base equality

PKU.3027

Description
Numerous are the moments I as a programmer have been frustrated by the tedious conversions between decimal numbers and hexadecimal ones. Why have we chosen 10 as a base in our everyday numerical presentations, when 16 seems so practically appealing? Obviously because everyone is not the computer geek I am. Maybe some day the world will fully realise the benefits of the hexadecimal system. In the meantime I have to learn to master the base conversions since most of the time numbers do not resemble one another in different bases.
Sometimes peculiar relationships emerge among the different base representations of numbers though. For instance, I noticed just the other day that 104010 * 4 = 104016, i.e. (1*103+0*102+4*101+0*100)*4=(1*163+0*162+4*161+0*160). It made me wonder how often this is the case, that is, the digits of a number in one base, are exactly the same as the digits of a multiple of the number in another base. Formally, let B1 < B2 be positive integers, and a0,a1,…,ak be integers in [0…B1-1]. For which ai’s is there a positive integer c such that 

Input
On the first line of input is a positive integer n telling the number of test cases that follow. Each test case is on a line of its own and consists of two integer bases B1, B2, 9 <= B1 < B2 <= 100, and two integer range elements, r1, and r2, 0 < r1 < r2 <= 10000. Notice that all numbers in the input are given in the base 10.

Output
For each test case, there should be one row containing the largest integer i, fulfilling r1 < i < r2, for which there is a positive integer c such that the digits of i in the base B1, are exactly the same as the digits of i*c in the base B2. If no such integer i exists, output the text ‘Non-existent.’.

Sample Input
4
10 16 1 2000
10 16 1 4999
10 14 10 9999
11 14 10 9999

Sample Output
1040
4240
Non-existent.
9240

Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

 

My Program

    #include < iostream >
    #include
< cmath >
    
using   namespace  std;
    
int  s[ 32 ] = {0} ,g;

    
void  Ten( long  x, int  d)
    
{
        
int h=1;
        
long y=0;
        g
=0;
        
if(x<0)
        
{
            x
*=-1;
            h
=-1;
        }

        
while(x>=d)
        
{
            s[g
++]=x%d;
            x
/=d;
        }

        s[g]
=x;
    }


    
int  main()
    
{
        
int b1,b2;
        
long t,m,z;
        
int n,i,j,r1,r2;
        cin
>>n;
        
while(n--)
        
{
            cin
>>b1>>b2>>r1>>r2;
            m
=0;
            
for(i=r2-1;i>r1;i--)
            
{
                z
=0;t=1;
                Ten(i,b1);
                
for(j=0;j<=g;j++)
                
{
                    z
+=s[j]*t;
                    t
*=b2;
                }

                
if(z%i==0)
                
{
                    m
=i;
                    
break;
                }

            }

            
if(m==0)
                cout
<<"Non-existent."<<endl;
            
else
                cout
<<m<<endl;
        }

        
return 0;
    }

 YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

 给定一个十进制数的范围r1~r2,找到一个数i使 r1 < i < r2,
 将i转换为b1进制下的数s,
 使s在十进制下的数乘于某个整数后与s在b2进制下转换的十进制数z相等。
 输出满足条件的最大的数,如不存在则输出Non-existent. 
i.e. (1*10^3+0*10^2+4*10^1+0*100)*4=(1*16^3+0*16^2+4*16^1+0*16^0).  => (1040)10 * 4 = (1040)16.

【粗略分析】

 要满足条件,其实只要i能够被z整除。
 由于输入的范围r1,r2是十进制数,
 我们先要将其中的数转换为b1进制下的数s,
 接着将s当作b2进制下的数运算成十进制下z,
 判断z是否能整除i即可。

 又因为题目要求输出满足条件的最大数,
 只需要从r2-1开始循环到r1+1,
 一旦找到数即可结束。

【C++源代码】

    #include < iostream >
    #include
< cmath >
    
using   namespace  std;
    
int  s[ 32 ] = {0} ,g;

    
void  Ten( long  x, int  d)
    
{
        
int h=1;
        
long y=0;
        g
=0;
        
if(x<0)
        
{
            x
*=-1;
            h
=-1;
        }

        
while(x>=d)
        
{
            s[g
++]=x%d;
            x
/=d;
        }

        s[g]
=x;
    }


    
int  main()
    
{
        
int b1,b2;
        
long t,m,z;
        
int n,i,j,r1,r2;
        cin
>>n;
        
while(n--)
        
{
            cin
>>b1>>b2>>r1>>r2;
            m
=0;
            
for(i=r2-1;i>r1;i--)
            
{
                z
=0;t=1;
                Ten(i,b1);
                
for(j=0;j<=g;j++)
                
{
                    z
+=s[j]*t;
                    t
*=b2;
                }

                
if(z%i==0)
                
{
                    m
=i;
                    
break;
                }

            }

            
if(m==0)
                cout
<<"Non-existent."<<endl;
            
else
                cout
<<m<<endl;
        }

        
return 0;
    }

【注意事项】

※ 好象不会有高精度,不过我蛮写了 = =


【点评】

好象没有什么好总结的 = = || 似乎进制转换还不熟练 Orz ……还要多加练习……

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值