POJ 1006 Biorhythms, poj1006biorhythms

Source: Internet
Author: User

POJ 1006 Biorhythms, poj1006biorhythms

POJ 1003,1004, 1005 is relatively simple and can be quickly solved. In an episode, I didn't quite understand ACM at the beginning. The most recent feedback on submitted problems was a Runtime Error. At first, I thought it was a timeout. On the one hand, I suspect that Java is running too slowly, then I learned that java is recommended for some international competitions, which means java is not slow. On the other hand, I suspect that my program is too bad and it takes a lot of time every time. So I optimize the code every time I encounter a Runtime Error, but sometimes I cannot optimize it. When I was doing POJ1005, the online code was pasted with AC, but I couldn't, but the two programs were already the same, at this time, I found that the culprit is that I always carry the package name when submitting (sometimes I manually did it ).


Although I can submit this question for POJ 1006, I have more methods for brute force cracking.

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 number=0; number<=21252+d; ){if(p == number % 23){if(e == number % 28){if(i == number % 33) {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 solving the problem is to abstract the essence of the problem from the complicated description, that is, to convert the phenomenon into a mathematical problem. The difficulty of POJ1006 is the mathematical problem behind it-the Chinese residue theorem.

A number can be divided into 3 to 2, 5 to 3, and 7 to 2. We can use the same equations in elementary number theory to solve the problem. Using the same symbol, we can convert the above problem into the same equations below:
X limit 2 (mod 3 );
X forward 3 (mod 5 );
X limit 2 (mod 7 );
It is not difficult to see that the solutions of the above same equations are not unique, because if x is a solution, x + 3*5*7 * k = x + 105k is also a solution of the same equations. As a matter of fact, we can see from the mutual quality of 3, 5, and 7 that the difference between any two solutions of the above same equations is a multiple of 105. How can we find the minimum solution of the above homogeneous equations? Our ancestors intelligently transformed the problem into the solutions of the following three very special systems of homogeneous equations:
A limit 1 (mod 3); B Limit 0 (mod 3); c limit 0 (mod 3 );
A limit 0 (mod 5); B Limit 1 (mod 5); c limit 0 (mod 5 );
A limit 0 (mod 7); B Limit 0 (mod 7); c limit 1 (mod 7 );
Then 2a + 3b + 2c is a solution of the original homogeneous equations (that is, 2a + 3b + 2c is divided by 3 to a, and 5 to B, if the number is less than 105, it is the minimum integer solution. After simple calculation, we can see that a can take 70, B can take 21, and c can take 15. The calculation result can be 2a + 3b + 2c = 233. The remainder 23 after dividing by 105 is the minimum positive integer solution.


Return to the POJ1006 question, number % 23 = p, number % 28 = e, number % 33 = I
So that 33*28 * a is divided by 23 and 1 can be 5544 = 33*28*6.
So that 23*33 * B is divided by 23 and 1 can be 14421 = 23*33*19
So that 23*28 * c is divided by 33 and 1 can be 1288 = 23*28*2
So there are (5544 * p + 14421 * e + 1288 * I) % lcm (23, 28, 33) = number
Due to the interoperability between 21252 and 33, lcm (23, 28, 33) =. So number = (5544 * p + 14421 * e + 1288 * I) % 21252, and the question is number-d. In this question, the minimum integer solution is used, to avoid the negative number of n, you must add 21252 when number-d is <= 0.

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.