UESTC 1639 Fruit Ninja (Idea question)

Source: Internet
Author: User

http://www.acm.uestc.edu.cn/problem.php?pid=1639

Ideas:

The key to this problem lies in the following two points:

1. M,n are very small: 1 <= m, n<= 50

2. The odds of all numbers being the same are very small, especially when the scores are high. In other words, it is rare to get an extra prize for N.

As a result, the priority analysis obtains an additional award for M, which is when the score is a multiple of 5.

Let's start with the circumstances that led to the INF:

1. When M is a multiple of 10, it can be added infinitely (because the end is 0 and all numbers cannot be the same).

2. When M is not a multiple of 10, but a multiple of 5, "almost" can be infinitely added, but there is a limit. For example, when the score is added to 55555, an extra n is required, which may cause the result to be no longer a multiple of 5. However, if n is also a multiple of 5, this problem does not occur.

Now consider the termination of a certain score:

1. When M is not a multiple of 10, but is a multiple of 5, and N is not a multiple of 5, the points that may be stuck appear in the 55555,555555,5555555,......

Set the initial score of 5a, then when 5a+k*m = 555...55 will be stuck, that is a+k* (M/5) = 111...11. So you can choose a to let the score avoid terminating in the smaller number as much as possible.

Due to m<=50, consider m=5,15,25,35,45.

m = 5,m/5=1, must terminate in 55555, result 55555+m+n

m = 15,m/5=3, a+3k=111...11 on both sides of the equivalent mode 3, that is, a%3=2,0,1,2,0,1,......。 We can choose A%3=1 's A, which will terminate at 7 1 here, so the result is 5555555+m+n

m = 25,m/5=5, the equivalent a+5k=111...11 on both sides of the mode 5, that is, A%5=1, select a a%5≠1 of a will not terminate, the result is INF.

m = 35,m/5=7, a+7k=111...11 7 on both sides of the peer, that is, the a%7=2,0,1,4,6,5,2,0,1,4,......, the selection of a a%7=3 is not terminated, and the result is INF.

m = 45,m/5=9, a+9k=111...11 9 on both sides of the peer, that is, the a%9=5,6,7,8,0,1,2,3,4,5,6,7,......, choose A%9=4 A, which terminates at 13 1, so the result is 5555555555555+m+n

2. When the m,n is not the case, the biggest situation is Max (9999+n+ (9999+n)%5?0:m), 10000+m), I believe everyone can understand the meaning of this sentence.

Complete code:

 01./*0ms,856kb*/02.  
#include <cstdio> 04. 05.int Main () 06.  
{A. int T, n, m, cas = 1, max;  
scanf ("%d", &t);    A for (; CAs <= T; ++cas) 10.  
{One. printf ("Case #%d:", CAs);  
scanf ("%d%d", &m, &n);  
If ((m%) = = 0) puts ("INF");        . else if ((m% 5) = = 0) 15.  
{. if ((n% 5) = = 0) puts ("INF");            . else 18.                {. switch (m) 20.  
{Case 5:printf ("%lld\n", 55555LL + n + m); Case 15:printf ("%lld\n", 5555555LL + n + m);  
Break Case 25:case 35:puts ("INF");  
Break  
Case 45:printf ("%lld\n", 5555555555555LL + n + m);            25.} 26.        } 27.        } 28.        Else 29.  
{max = 9999 + N;  
if (max% 5 = 0) max = m; if (10000 + M > max) max = 10000 + M;  
printf ("%d\n", Max);    34.} 35.    } 36.  
return 0; Notoginseng.} 

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.