[Zoj] 3785 what day is that day? -- Talking about the rule of brute force table search in the ACM competition of KMP applications

Source: Internet
Author: User

First, declare that the rule here refers to a loop, that is, finding the minimum cycle. I am sure you know this. "It's not just an application of the next array ".

Let's take a look at a question.

Zoj 3785

What day is that day? Time Limit: 2 seconds memory limit: 65536 KB

It's Saturday today, what day is it after 11 + 22 + 33 +... +NNDays?

Input

There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:

There is only one line containing one integerN(1 <=N<= 1000000000 ).

Output

For each test case, output one string indicating the day of week.

Sample Input
212
Sample output
SundayThursday
Hint

A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

 

The general idea of this question is that today is Saturday. Please ask f = 11 + 22 + 33 +... +NNThe day of the week after so many days.

That is, evaluate F % 7 for each input N value. This question is found on the Internet, and it is said to be the rule of table searching. Of course, there are two ways to find this question: one is for each IIFind the rule for values of % 7.

The first 100 values are shown in the table below.

1 4 6 4 3 1 0 1 1 4 2 1 6 0 1 1 5 1 1 0 1 1 4 4 4 4 6 0 1 1 3 2 6 1 0 1 2 2 2 2 2 6 0 1 4 6 4 3 1 1 1 1 4 2 1 6 0 1 2 5 1 5 1 1 0 1 1 4 4 4 6 0 1 1 3 2 6 1 0 1 2 2 1 2 6 0 1 4 4 3 1 1 1 1 4 2 1 6 0 1 2

One way to find the rule is to mark the number when it is equal to the first number, and then manually determine whether a cycle can be formed.

It is undeniable that it is not difficult to find a set of numbers with a short cycle. However, if the cycle is as large as several hundred or even tens of thousands, it may be a drop in the water.

At that time, I was lost in this long string of numbers, and suddenly remembered the nature of the next array in KMP, which I recently saw. I immediately thought of using KMP to find the minimum length of the repeated substring, so the brain holes are wide open ......

This property is: Set J = Leni-next [I]. If I % J = 0 and I/j> 1, J is the smallest cyclic section of PI (PI indicates the first I character of the text string, Leni indicates the length of the string, generally expressed as Leni = I + 1)

The key code is as follows:

/* Note: int next [] is the next array, int arr [] is the array for finding a regular, and Len is the array length */next [0] = 0; for (INT I = 1, q = 0; I <Len; I ++) {While (q> 0 & arr [I]! = Arr [Q]) q = next [q-1]; If (ARR [I] = arr [Q]) q ++; next [I] = Q; if (Q! = 0 & (I + 1) % (I + 1-q) = 0) {printf ("% d \ n", I + 1-Q); break ;}}
View code

The minimum cycle is 42.

 

Another way to find the rule is to directly search for the value of F % 7 in the table, and the cycle found according to the above method is 294. The following is very simple ~

The AC code is as follows:

 1 #include <cstdio> 2  3 const char day[10][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 4 int s[300]; 5  6 int work(int n) 7 { 8     int sum = 1; 9     for(int i = 1; i <= n; i++){10         sum = sum * n;11         sum %= 7;12     }13     return sum;14 }15 16 void init()17 {18     s[0] = 0;19     for(int i = 1; i <= 294; i++){20         s[i] = s[i-1] + work(i);21         s[i] %= 7;22     }23 }24 25 int main()26 {27     int T;28     int n;29     init();30     scanf("%d", &T);31     while(T--){32         scanf("%d", &n);33         n %= 294;34         printf("%s\n", day[ s[n]]);35     }36     return 0;37 }
View code

 

----------------------------------------------------------- I am a split line --------------------------------------------------------------

Since the last time I opened my mind, I encountered another question today, poj 3070.

The general idea of the question is to ask you how many are the last four digits of the nth Fibonacci number. The question is intended to evaluate the power of the matrix quickly. But what can be done by brute force tabulation? Then we used KMP to query the table and found that the cycle was 15000. The problem was solved successfully ~

 

Well, this article is just to share with you the usage of the KMP that you accidentally discovered in the competition, in some cases, it is tempting to use them (just like the poj 3070 benchmark). However, no matter whether it is speculative or not, it is a good way to use them as long as they can communicate with each other. O (∩) O Haha ~ Make a joke. If you have any shortcomings, please correct them.

 

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.