Codeforces round #183 (303c)-Minimum Modular

Source: Internet
Author: User

Two numbers A and B are the same as the remainder M. Obviously (a-B) % m = 0 ..... if M % P = 0... so (a-B) % P = 0 ....

Record all (A [J]-A [I... use a table in C [1000000] to record it... if a number p is the same remainder of the N logarithm .. so C [p] = n...

With the above processing, you can conveniently calculate the MD for the current enumeration... how many pairs will be generated for the same remainder (add all the Integers of MD to C )...

But it does not mean that the number of N-1 needs to be removed. For example...

3, 6, 9, 10 .... (3, 6), (3, 9), (3, 12), (6, 9), (6, 12), (9, 12 )... if the MD of the current test is 3 .. there will be 6 pairs with the remainder of 3 .... in fact, at least three digits are subtracted to ensure that the remainder 3 does not exist...

I have no idea here...

I read other people'sCode... I understood it... it makes sense... but it also feels bold...

In fact, it is not the case that MD is used to calculate the remainder of the same number minus a few numbers .. it is used for pruning .. and then look for brute force attacks (pure brute force .. one by one .. check the number of duplicates .. whether the number of duplicates is not greater than K)

The pruning method is the current MD cool pair> K * (k + 1)/2... find the next MD ....

Why do we not need to use the brute force method to determine the number of K * (k + 1)/2?

Consider an extreme situation .. each number of N is an integer multiple of MD .. obviously, there are N * (n-1)/two same-remainder pairs about MD... however, you only need to remove the n-1 number so that there is no same result...

Think of N-1 as the k... required by the question. Isn't the extreme case that the equivalence pair of MD is (k + 1) * k/2? When the same-remainder pair of MD is> (k + 1) * k/2, delete k + 1 in any case to make the same-remainder pair of MD appear...

This pruning is quite exciting .. although each MD that has passed the same remainder pair <= (k + 1) * k/2 verification must undergo a brute force lookup and judgment... but the time is completely OK...

Program:

 

# Include <iostream> # include <stdio. h> # include <cmath> # include <string. h >#include <algorithm> # include <queue> # include <stack> # define ll long # define oo 1000000000 using namespace STD; int A [5005], c [1000005], n; bool used [1000005]; int main () {int I, J, K, M; while (~ Scanf ("% d", & N, & K) {for (I = 1; I <= N; I ++) scanf ("% d ", & A [I]); sort (a + 1, A + 1 + n); memset (C, 0, sizeof (c); for (I = 1; I <n; I ++) for (j = I; j <= N; j ++) C [A [J]-A [I] ++; for (I = 1; I <= A [n] + 1; I ++) {m = 0; For (j = I; j <= 1000000; J + = I) {M + = C [J]; If (M> K * (k + 1)/2) goto a;} memset (used, false, sizeof (used); M = 0; For (j = 1; j <= N; j ++) {If (used [A [J] % I]) M ++; used [A [J] % I] = true; If (M> K) goto a;} break; :;} printf ("% d \ n", I);} 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.