Fjnu 1153 fat brother and xor (fat Brother with different or)

Source: Internet
Author: User

Fjnu 1153 fat brother and xor (fat Brother with different or)

Time limit:1000ms Memory limit:257792k

"Description"

"Title description"

Fat brother had master ACM, recently he began to study the operation of XOR (the operation "^"). He thought of a very interesting question:select arbitrary k positive integers from the n positive integers and then XOR the Selected K Digital, sum all cases of XOR. Now he wants to quickly calculate the results. Maybe the results would be great, just modulo 20162333.

For example, 3 integers:1 2 3, select arbitrary 2 positive integers, the all cases:

{2, 3}, {1, 3}. So the results is {(1 ^ 2) + (2 ^ 3) + (1 ^ 3)}%20162333

Fat elder brother is an ACM Daniel, recently he is learning XOR or operation (operator is "^"). He came up with an interesting question: choose K from n positive integers, and then add to the number of K or all the different results. Now he wants to calculate the result quickly. The end result can be very large, so modulo 20162333.

For example, 3 integers: 1 2 3, select any 2 positive integers, in all cases:

{2, 3}, {1, 3}. So the result is {(1 ^ 2) + (2 ^ 3) + (1 ^ 3)}%20162333

"Input"

Input

There is multiple test cases. The first line of input contains an integer t (t <=) indicating the number of test cases. For each test case:

The first line contains both integer n, K (1 <= k <= n <= 1000)

The second line contains n integer ai (1 <= ai <= 1000000000)

Multiple sets of test cases.

The first line is an integer t (T <= 20) that represents the number of test cases. For each test case:

The first line has two integers, K (1 <= k <= n <= 1000)

The second line has n integers ai (1 <= ai <= 1000000000)

"Output"

Output

For per test case, output the sum% 20162333

For each test case, the output and%20162333

"Sample Input-Enter sample"

"Sample output-Output sample"

2

3 2

1 2 3

6 2

6 6 6 6 6 6

6

0

"Hint"

Prompted

The

First sample test Case: (1 ^ 2) + (2 ^ 3) + (1 ^ 3) = 6

The

Second sample test Case: (6 ^ 6) * 15 = 0

The first example:

(1 ^ 2) + (2 ^ 3) + (1 ^ 3) = 6

A second example:

(6 ^ 6) * 15 = 0

Exercises

This problem requires a binary view of each AI

As an example:

Xai represents the X-bit of the lower AI of the binary (for convenient representation starting from 0)

(A1 ^ A2) + (A2 ^ A3) + (A1 ^ A3)

=[(0a1 ^ 0a2) + (0a2 ^ 0a3) + (0a1 ^ 0a3)]*20 +

[(1a1 ^ 1a2) + (1a2 ^ 1a3) + (1a1 ^ 1a3)]*21 +

[(2a1 ^ 2a2) + (2a2 ^ 2a3) + (2a1 ^ 2a3)]*22 +

....... ..... ................. ......... ....... ..... ....................... +

[(Xa1 ^ Xa2) + (Xa2 ^ Xa3) + (Xa1 ^ Xa3)]*2x

There's only 0 and 1 left.

We can easily know: 0 ^ 0 = 0, 1 ^ 0 = 1, 1 ^ 1 = 0

Of course, a ^ b = b ^ a

So in addition to 1 ^ 0, the other result is 0 of the situation is useless.

Therefore, the title is converted to: for binary, 1 ^ 0 The number of occurrences of this situation.

Statistics AI binary X-bits appear several times 1, save to Bit[x]

1 = 2X * Available in X-bit * Number of active cases

The number of valid cases is a combination of bit[x] 1 and n-bit[x] 0.

And only an odd number of 1, the result of the XOR can be 1 (1 ^ 1 = 0)

From this we can draw

Current number of valid cases:

Finally, we use recursive formula to find the combination number quickly.

(Of course you can think of this formula as a Yang Hui triangle is also possible:))

"Code C + +"

1#include <cstdio>2#include <cstring>3 #defineMX 10054 #defineMoD 201623335 intMain () {6     intT, N, K, I, J, W, opt, bit[ -], c[mx][mx];7      for(i =0; i < MX; ++i) {8          for(c[i][0] = C[i][i] = j =1; J < I; ++j)9C[I][J] = (C[i-1][J] + c[i-1][j-1]) %MoD;Ten     } One      while(~SCANF ("%d", &t)) { A          while(t--){ -memset (bit,0,sizeof(bit)); -scanf"%d%d", &n, &k); the              for(i =0; I < n; ++i) { -scanf"%d", &j); -                  for(w =0; J J >>=1) bit[w++] + = j &1; -             } +              for(i = opt =0; I < -; ++i) { -                  for(j =1; J <= K; J + =2){ +opt + = (1LL << i) *c[bit[i]][j]% mod*c[n-bit[i]][k-j]%MoD; AOpt%=MoD; at                 } -             } -printf"%d\n", opt); -         } -     } -     return 0; in}
1#include <cstdio>2#include <cstring>3 #defineMX 10054 #defineMoD 201623335 intRead_int () {6     intAdd = GetChar ()-'0';7     intA =GetChar ();8      while(A >='0'&& a <='9') Add = Add *Ten+ A-'0', a =GetChar ();9     returnadd;Ten } One intMain () { A     intT, N, K, I, J, W, opt, bit[ -], *C[MX]; -      for(i =0; i < MX; ++i) { -C[i] =New int[i +1]; the          for(c[i][0] = C[i][i] = j =1; J < I; ++j) -C[I][J] = (C[i-1][J] + c[i-1][j-1]) %MoD; -     } -      while(t = Read_int (), t>0){ +          while(t--){ -memset (bit,0,sizeof(bit)); +n = read_int (); K =read_int (); A              for(i =0; I < n; ++i) { at                  for(w =0, j = Read_int (); J J >>=1) bit[w++] + = j &1; -             } -              for(i = opt =0; I < -; ++i) { -                  for(j =1; J <= K; J + =2){ -                     if(Bit[i] < J | | n-bit[i] < K-J)Continue; -opt + = (1LL << i) *c[bit[i]][j]% mod*c[n-bit[i]][k-j]%MoD; inOpt%=MoD; -                 } to             } +printf"%d\n", opt); -         } the     } *     return 0; $}
pseudo • Optimization


Fjnu 1153 fat brother and xor (fat Brother with different or)

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.