Baidu Star Qualifying Problem 1001

Source: Internet
Author: User

Test instructions

The bear has a dictionary that stores a large number of words, and once, he makes all the words a long, long string. Now that the trouble has come, he forgets what the original string is, and what is magical is that he remembers the hash of the original string. The hash value of a string, calculated by the following formula:

Please help the bear calculates the number of hashes in any segment of a large string. Multiple sets of test data, each set of test data the first line is a positive integerN, representing the number of queries, the second line of a string, representing the large string in the title, and thenn < Span class= "Mord mathit" style= "MARGIN-RIGHT:0.10903EM;" > row, each row contains two positive integers a and B

Ideas:first, use a prefix array a[i] to represent the H (s) value of the string from the first character to the I-character, and, if required from A to B, it is obviously a[b]/a[a-1]. Also because the A array is 9973 modulo, so the equivalent is known a[b]% p, a[a-1]% p, ask A[B]/a[a-1]. known a% mod, b% mod, request (A/b)% mod, with inv (p) to represent P's inverse
(A/b)% mod = (A * INV (b))% mod = (a% mod * INV (b)% mod)% MoD;
Then ask for 1 to mod-1 mod inverse to inv (b% MoD)
So (A/b)% mod = (a% mod * INV (b% mod))% MoD;
Ask 1 to mod-1 for mod inverse code:
 const  int  mod = 9973  ; 2  LL inverse[mod + ]; 3  inverse[0 ] = 0 , Inverse[1 ] = 1   4   int  i = 2 ; i < mod; I++)  5  inverse[i] = (-(mod/i) * Inverse [MoD% i]) % mod + mod;  

This problem code:

1 Const intMAXN =1000000+7;2 Const intp =9973;3 LL ni[p];4 CharSTR[MAXN];5 LL A[MAXN];6 7 intMain () {8     //freopen ("Input.txt", "R", stdin);9ni[1] =1;Tenni[0] =0; One      for(inti =2; I < P; i++) ANi[i] = (-(p/i) * ni[p%i])% p +p; -     intN; -      while(~SCANF ("%d", &N)) the     { -scanf"%s", str); -         intLen =strlen (str); -a[0] =1; +          for(inti =1; I <= Len; i++) -A[i] = a[i-1] * (Str[i-1] - -) %p; +          while(n--){ A             ints, m; atscanf"%d%d", &s, &m); -printf"%i64d\n", A[m] * ni[a[s-1]] %p); -         } -     }  -     return 0; -}

Baidu Star Qualifying Problem 1001

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.