ACM Elective HUST1058 (city title) Lucky Sequence congruence theorem

Source: Internet
Author: User


Edward gets a sequence of integers of length N, and he wants to find out how many of these "lucky"
Continuous sub-sequences. A continuous sub-sequence is called "lucky" when and only if the integer within that subsequence
And exactly the integer multiples of K. He asks you to write a program that calculates the number of successive subsequence sequences he likes.


The input first line is an integer t, which indicates that there is a T group of data.
The first row of each group of data is two integers n (1 <= n <= 10^6), K (1 <= k <= 10^9).
The next line contains N integers Ai (| ai| <= 10^9).


For each set of test data, the output line contains only one integer representing the successive sub-order Edward likes
The number of columns.

Sample Input
25 31 2 3 4 16 21 2 1 2 1 2
Sample Output

At first I did not think of the same remainder theorem, according to the Nanako of the great God, can be almost just card time and memory AC, but 鶸 does not.

Remember that after the simulation game, Nanako to the problem of the brain hole is almost completely close to the positive solution.

Analysis: According to the congruence theorem:(A-B)%c=a%c-b%c, you can think of using the prefix and, and record each prefix and the value after the modulo K,O (n^2) solution is a two-layer cyclic prefix array modulo k sets of formula, obviously timed out (good stupid , And then find several numbers with the same remainder. Example two: The prefix and the array after the modulo K is 1 1 0 0 1 1, find the remainder of the same b[0],b[1],b[4],b[5],4, then take the first and the end, the combination of different positions have (3+1) * (3)/2=6, the same, 0 have 2, However, note that the remainder of 0 means that the prefix of the position and itself is a multiple of k, so the combination of 0 is (2+1) *2/2=3.

Note the point: The number given may be negative, so the remainder needs to be transformed.

1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 8 inta[1000010], b[1000010];9 intXe (inta)Ten { One     return(A +1) * (a)/2; A } - intMain () - { the     intT, T; -     intN, K; - ll x, ans; -scanf"%d", &T); +          while(t--) -         { +scanf"%d%d", &n, &k); A  atans=0; -scanf"%d", &b[0]); -b[0]%=K; -             if(b[0] <0)//Note Negative Transform -b[0]= (b[0]+K)%K; -  in  -              for(intI=1; i<n; i++) to             { +scanf"%d",&a[i]); -b[i]=b[i-1]+A[i]; theb[i]%=K; *                 if(b[i]<0)//Note Negative Transform $b[i]= (b[i]+k)%K;Panax Notoginseng             } -Sort (b, b+n);//easy handling after sorting theb[n]=-1; +             /*for (int i=1; i<=n; i++) A             { the cout<<b[i]< ""; +             } -  $ for (int i = 1; I <= n; i++)//o (n^2) obviously t Yes Brother $             { - For (int j = 0; J < i; j + +) -                 { the if (! ( (k+b[i]%k-b[j]%k)%k)) -                     {Wuyi ans++; the //printf ("%d~%d\n", i,j); -                     } Wu                 } -             }*/ Aboutx=1; $              for(inti =1; I <= N; i++) -        { -                 if(b[i]==b[i-1]) -X + +; A                 Else +          { the                     if(b[i-1]!=0)//not 0 of the situation -x--; $Ans+=xe (x); thex=1; the          } the        } theprintf"%lld\n", ans); -       } in}

ACM Elective HUST1058 (city title) Lucky Sequence congruence theorem

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: 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.