POJ 1006 Biorhythms

Source: Internet
Author: User

POJ 1003,1004,1005 is relatively simple and soon solved. There is a small episode, just beginning to do not understand the ACM, the most recent submission of the problem is the runtime Error, the beginning I thought it was timed out, on the one hand I suspect is not Java run too slow, and then to understand that some international competitions recommended Java, that the Java itself is not slow. On the other hand I suspect that my program is too bad, each time is very good time-consuming, so every time the runtime error problem to optimize the code, but how to optimize or not. Finally, the POJ1005 online code affixed to the AC, but I still do not, this time only to find the culprit is when I submit always with the package name (of course, sometimes manually).


POJ 1006 This topic although I can submit through, but my method more calculate brute force hack.

Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in ); for (int j=1; j<integer.max_value; J + +) {int p = scanner.nextint ()% 23;int e = scanner.nextint ()% 28;int i = scanner. Nextint ()% 33;int d = scanner.nextint (); if (p==-1 && e==-1 && i==-1 && d==-1) return;for (int numbe r=0; number<=21252+d; {if (p = = number%) {if (e = = number%) {if (i = = number%) {if (number-d <= 0) Number + = 21252; SYSTEM.OUT.PRINTLN ("Case" +j+ ": The next triple peak occurs in" + (number-d) + "days."); break;} Elsenumber + = 23*28;} else number + = 23;} else number + +;}}}}

The key to the problem is to abstract the essence from the complex phenomenon, that is, to transform the topic into a mathematical problem. The difficulty of this problem is the mathematical problem behind--the Chinese remainder theorem.

This topic is known as number% at p,number = e,number% = I

Make 33*28*a 23 to 1, 5544 = 33*28*6

Make 23*33*b 23 to 1, 14421 = 23*33*19

Make 23*28*c 33 to 1, 1288 = 23*28*2

Therefore there is (5544*p + 14421*e + 1288*i)% LCM (at a total of.) = number

Also due to 23,28,33 coprime, the LCM (23, 28, 33) = 21252. So number = (5544*p + 14421*e + 1288*i)% 21252, the request is number-d, the subject is the smallest integer solution, to avoid n is negative need to add 21252 when the number-d<=0.

POJ 1006 Biorhythms

Related Keywords:

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.