2016 "Baidu Star"-Qualifying (Astar ROUND1)-(analog + segment tree + multiplicative inverse)

Source: Internet
Author: User
Tags hash time limit
problem Aaccepts:1351 submissions:9951 time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) Pr Oblem Description

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:

H (s) =\prod_{i=1}^{i\leq Len (s)} (s_{i}-28) \ (mod\ 9973) H (s) =∏i=1 I≤len (s) (S i−28) (mod 9973)

S_{i}s I represents the ASCII code for the s[i] character.

Please help the bear calculates the number of hashes in any segment of a large string. Input

Multiple sets of test data, each set of test data the first line is a positive integer nn, representing the number of queries, the second line of a string, representing the large string in the topic, the next nn line, each line contains two positive integers AA and BB, which represents the starting position of the query and the ending position.

1\leq N\leq 1,0001≤n≤1,000

1\leq len (String) \leq 100,0001≤len (String) ≤100,000

1\leq A,b\leq len (String) 1≤a,b≤len (string) Output

For each query, output an integer value that represents the hash value of the substring of the large string from the AA bit to the BB bit. Sample Input

2
ACMlove2015
1
8
1
testMessage
1 1
Sample Output
6891
9240
88
Statistic |  Submit |  Clarifications | Back the first is a multiplication inverse, what is a multiplication inverse, which is to turn a division operation into a multiplication operation, as follows: Solution (B/A) mod P--(b * x) mod p and the x = a ^ (PHI (p)-1), and if p is prime number, phi (P) = P-1, so x = a ^ (p-2). {phi () is Euler's function}, the formula becomes (b * (a ^ (p-2))) mod p, where a^ (p-2), Quick power template skipped. So we will use a lost and lost the prefix and the idea of the product to save the remainder in the array, and then use the above formula directly into the solution can be
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 5;
int H[MAXN];
Char HSTR[MAXN];
int N, L, R;
const int mods = 9973;
typedef long long LL;

ll Mod_pow (ll X, ll N, ll MoD) {
    ll res = 1;
    while (n > 0) {
        if (N & 1) res = res * x% MoD;
        x = x * x% mod;
        n >>= 1;
    }
    return res;
}


int main () {while
    (~scanf ("%d", &n)) {
        scanf ("%s", hstr);
        int len = strlen (hstr);
        H[0] = 1;
        for (int i = 1;i <= len;i + +) {
            H[i] = h[i-1] * (Hstr[i-1]--)% mods;
        }
        while (N-) {
            scanf ("%d%d", &l, &r);
            if (L > R) Swap (L, R);
            printf ("%i64d\n", (LL) h[r] * MOD_POW (H[L-1], mods-2, mods)% mods);
        }
    }
    return 0;
}


problem Baccepts:1809 submissions:6745 time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) Pr Oblem Description

In front of the bear there is a string consisting entirely of 1, called the full 1 sequence. You can merge any adjacent two 1 to form a new sequence. For a given full 1 sequence, calculate how many different sequences can be formed according to the above methods. Input

This includes several sets of test data, each set of test data containing a positive integer nn, representing the length of the whole 1 sequence.

1\leq N \leq 2001≤n≤200 Output

For each set of test data, output an integer representing the number of new sequences that can be formed by the full 1 sequence given in the topic. Sample Input

1
3
5
Sample Output
1
3
8

Hint If the sequence is: (111). The following three new sequences can be constructed: (111), (21), (12).  Statistic |  Submit |  Clarifications | Back
The second question is a Fibonacci sequence, dp[i] = Dp[i-1] + dp[i-2]. Of course, it can not be used DP, but the pure mathematical solution, because we can know by observing the topic, the problem is nothing more than solving 0 2 how to put, a 2 how to put, 3 2 how to put ... So is the number of combinations, C (n,m) = C (n-1,m-1) +c (n-1,m), the solution, relative to the length of N 2 of the number of placement method {because each produces a 2 total length will be reduced by one, so the total number of the display is n-i,i represents 2 of the number, n is the number of places where 2 can be placed} of course, this is obviously a large number, so the Kuangbin large number template borrowing, so the code is as follows: Fibonacci sequence:
#include <stdio.h> #include <string> #include <string.h> #include <iostream> using namespace std

;
    Compare comparison function: Equals returns 0, greater than return 1, less than return-1 int compare (string str1,string str2) {if (Str1.length () >str2.length ()) return 1;
    else if (Str1.length () <str2.length ()) return-1;
else return Str1.compare (STR2);

    }//High-precision addition//can only be two positive numbers added string add (String str1,string str2)//high-precision addition {string str;
    int Len1=str1.length ();
    int Len2=str2.length ();
    Front 0, make the same length if (len1<len2) {for (int i=1;i<=len2-len1;i++) str1= "0" +STR1;
    } else {for (int i=1;i<=len1-len2;i++) str2= "0" +STR2;
    } len1=str1.length ();
    int cf=0;
    int temp;
        for (int i=len1-1;i>=0;i--) {temp=str1[i]-' 0 ' +str2[i]-' 0 ' +cf;
        CF=TEMP/10;
        temp%=10;
    Str=char (temp+ ' 0 ') +str;
    } if (cf!=0) Str=char (cf+ ' 0 ') +str;
return str; }//high-precision subtraction//can only be subtracted from two positive numbers, and a large reduction in string sub (string str1,string sTR2)//high-precision subtraction {string str;
    int tmp=str1.length ()-str2.length ();
    int cf=0; for (int i=str2.length () -1;i>=0;i--) {if (STR1[TMP+I]&LT;STR2[I]+CF) {Str=char (str1[tmp
            +i]-str2[i]-cf+ ' 0 ' +10) +str;
        cf=1;
            } else {Str=char (str1[tmp+i]-str2[i]-cf+ ' 0 ') +str;
        cf=0; }} for (int i=tmp-1;i>=0;i--) {if (str1[i]-cf>= ' 0 ') {Str=char (STR1[I]-CF) +s
            Tr
        cf=0;
            } else {Str=char (str1[i]-cf+10) +str;
        cf=1;
}} str.erase (0,str.find_first_not_of (' 0 '));//Remove excess leading 0 return str from the result;
    }//high-precision multiplication//can only be two positive multiplication of string Mul (String str1,string str2) {string str;
    int Len1=str1.length ();
    int Len2=str2.length ();
    String TempStr;
        for (int i=len2-1;i>=0;i--) {tempstr= "";
        int temp=str2[i]-' 0 ';
        int t=0;
        int cf=0;
       if (temp!=0) {for (int j=1;j<=len2-1-i;j++) tempstr+= "0";
                for (int j=len1-1;j>=0;j--) {t= (temp* (str1[j]-' 0 ') +cf)%10;
                Cf= (temp* (str1[j]-' 0 ') +CF)/10;
            Tempstr=char (t+ ' 0 ') +tempstr;
        } if (cf!=0) Tempstr=char (cf+ ' 0 ') +tempstr;
    } str=add (STR,TEMPSTR);
    } str.erase (0,str.find_first_not_of (' 0 '));
return str; }//High-precision division//Two positive divisions, quotient is quotient, remainder is residue//requires high precision subtraction and multiplication void div (string str1,string str2,string "Ient,string &residue
        {quotient=residue= "";//clear if (str2== "0")//Determine whether the divisor is 0 {quotient=residue= "ERROR";
    Return
        } if (str1== "0")//Determine if dividend is 0 {quotient=residue= "0";
    Return
    } int Res=compare (STR1,STR2);
        if (res<0) {quotient= "0";
        RESIDUE=STR1;
    Return
        } else if (res==0) {quotient= "1"; Residue=&q

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.