Primary school game-second "I Am a programmer" Nankai University Online Program Design Competition

Source: Internet
Author: User
Tags 0xc0

One day, the boring little Jie called on several students to play games, including silly little phoenix, silly little snow, cute little Xin and self-righteous little exercises. They are looking for clever little tricks to serve as referees for them. Determine who wins the game. In this game, Xiaoyi gives a number a and a number B. After a specified operation, this game converts number A to number B and uses the minimum number of transformations n. whoever talks about this n first wins. Of course, because they are all primary school students, it is assumed that if n> 6, there is no answer. Then, the referee Xiaoyi tries to program and obtain the answer as soon as possible. Please help him ......
Problem description:
Number A (A is a positive integer with no more than 50 digits), and number B (also a positive integer with no more than 50 digits ),
There are three types of operations:
1: Add 1985429 to the current number
2: Add 2006 to the current number
3: multiply the current number by 2

You need to find the minimum n. If n> 6, the output is-1. (This is negative 1 ).
Example 1: Xiaoyi provides numbers a = 1 and numbers B = 1987437.
So we can change 1 to 1987437 after three specified operations as soon as possible.
1*2 = 2; (3rd transformations)
2 + 1985429 = 1985431; (1st transformations)
1985431 + 2006 = 1987437; (2nd transformations)
Example 2: Xiaoyi provides numbers a = 1 and numbers B = 128.
So we can change 1 to 128 after 7 specified operations as soon as possible.
1*2*2*2*2*2*2*2 = 128 (both use 3rd transformations), but because n> 6, output-1 according to the meaning of the question.

Input

The input contains only two integers, A and B. Each row has a number. The 0 output has only one row, that is, the minimum number of transformations is N. If n> 6, the output is-1.
Note that there is a line break at the end.
Sample Input

11987437
Sample output
3
Hint

[Second session "I Am a programmer" [final data]

At the beginning, I misunderstood the meaning of the question. I thought it was 50-bit binary data, but actually it was 50-bit 10-digit data. So I cannot use long data ~~, Paste the AC code

# If 0/* For vc6.0 */

Typedef unsigned _ int64 u64;

# Else/* for GCC */

Typedef unsigned long u64;

# Endif

# Include
# Include
Static u64 o, A, B;

Static int I, J, K, Count = 7, tmp_cnt, C [] = {3, 0xc, 0x30, 0xc0, 0x300, 0xc00 };

Static char so [53], sa [53], Sb [53];

Static char * s1985 = "9245891 ";

Static char * s2006 = "6002 ";

Static void add_add (char * ss_tmp)

{

Int I, Len = strlen (ss_tmp), add = 0, tmp_add = 0;

For (I = strlen (SA); I Sa [I] + = 48;

For (I = 0; I {

Tmp_add = add;

Add = (SA [I] + tmp_add + ss_tmp [I]-96)/10;

Sa [I] = (SA [I] + tmp_add + ss_tmp [I]-96) % 10 + 48;

}

If (ADD)

{

Len = strlen (SA );

For (; I {

Tmp_add = add;

Add = (SA [I] + Add-48)/10;

Sa [I] + = tmp_add;

}

If (ADD)

Sa [I] = add + 48;

}

}

Static void nothing (){}

Static void add1985429 () {add_add (s1985 );}

Static void add2006 () {add_add (s2006 );}

Static void double2 () {add_add (SA );}

Typedef void (* funptr )();

Static funptr fun [] = {add1985429, add2006, double2, nothing };

Static convert (char * s)

{

Char TMP;

Int I, Len = strlen (s );

For (I = 0; I {

TMP = s [I];

S [I] = s [len-1-i];

S [len-1-i] = TMP;

}

}

Int main ()

{

Scanf ("% S % s", so, Sb );

Convert (so );

Convert (SB );

For (I = 0; I {

Memcpy (SA, so, 53 );

Tmp_cnt = 0;

For (j = 0; j fun [(I & C [J])> (J If (strcmp (SA, Sb) = 0)

{

For (j = 0; j if (I & C [J])> (J tmp_cnt ++;

If (tmp_cnt COUNT = tmp_cnt;

}

}

If (count! = 7)

Printf ("% d/N", count );

Else

Printf ("-1/N ");

Return 0;

}

The following is the code that was previously written for misunderstanding. You can use the system's eight test cases. The other two must be more than 64-bit binary numbers.

# Include
Typedef unsigned long u64;

Static u64 o, A, B;

Static void nothing (){};

Static void add1985429 () {A + = 1985429 ;};

Static void add2006 () {A + = 2006 ;};

Static void double2 () {atypedef void (* funptr )();

Static funptr fun [] = {add1985429, add2006, double2, nothing };

Static I, J, K, Count = 7, tmp_cnt, C [] = {3, 0xc, 0x30, 0xc0, 0x300, 0xc00 };

Int main ()

{

Scanf ("% i64d % i64d", & O, & B );

For (I = 0; I {

A = O;

Tmp_cnt = 0;

For (j = 0; J {

K = (I & C [J])> (J fun [k] ();

}

If (A = B)

{

For (j = 0; j if (I & C [J])> (J tmp_cnt ++;

If (tmp_cnt COUNT = tmp_cnt;

}

}

If (count! = 7)

Printf ("% d/N", count );

Else

Printf ("-1/N ");

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.