201,600-degree star qualifying problem A (prefix product and inverse element)

Source: Internet
Author: User

  Test instructions: Gives a string, each asking for x and y to calculate theproduct (mod9973) of the value (ASCII code value-28) of each character from X to Y.

Analysis: The first idea must be to figure out the prefix product for each location, and then just f[y]/f[x-1]. However, each prefix product has been mod9973, it can not directly come to the result, so the use of inverse element. Since A/b (mod p) p is an odd number, it is equivalent to A*INV (b) (mod p), so just save the prefix product and the inverse of the prefix product for each location.

But the topic has a pit point, if X and y beyond the position of this string, you can use the last data stored value instead of 0, because the topic given x and Y is not more than the length of the string, so every time I give the input data I memset again, resulting in this place WA several times.

At the same time, here is a trick is, since the inverse is mod9973, so as long as the storage of 0 to 9972 of the inverse to do a preprocessing can save a lot of time. See the code for details:

1#include <stdio.h>2#include <algorithm>3#include <string.h>4 using namespacestd;5 Const intMoD =9973;6 intInvintAintb//B is mod-27 {8     intans=1;9      while(b)Ten     { One         if(b&1) ans= (ans*a)%MoD; Ab>>=1; -a=a*a%MoD; -     } the     returnans; - } - Chars[100000+Ten]; - intnum[100000+Ten]; + intp[100000+Ten]; - intres[10000]; + intMain () A { at     intT; -      for (int i=1; i<mod;i++) RES[I]=INV (i,mod-2);  -      while(SCANF ("%d", &t) = =1) -     { -scanf"%s", s+1); -         intLen =strlen (s+1); in         //memset (num,0,sizeof (num)); -         //memset (P,0,sizeof (P)); tonum[0]=p[0]=1; +          for(intI=1; i<=len;i++) -         { thenum[i]=num[i-1]* (s[i]- -)%MoD; *             p[i]=Res[num[i]];  $         }Panax Notoginseng          while(t--) -         { the             intx, y; +scanf"%d%d",&x,&y); Aprintf"%d\n", num[y]*p[x-1]%MoD); the         } +     } -     return 0; $}

Note The code in the Red section.

201,600-degree star qualifying problem A (prefix product and inverse element)

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.