HDU - 5685 Problem A

1.题面

http://acm.hdu.edu.cn/showproblem.php?pid=5685

2,题意

中文题不解释

3.思路

一道求逆元的题目,先用O(n)的时间求出每个位置处的hash值

对于每个输入a和b

ans = hash(b)/hash(a-1);

这里需要使用求逆元的知识,

用扩展欧几里德,或是费马小定理

我写的是费马小定理的版本,下次写一个欧几里德的版本

4.代码

/*****************************************************************
    > File Name: Cpp_Acm.cpp
    > Author: Uncle_Sugar
    > Mail: [email protected]
    > Created Time: 2016年05月15日 星期日 14时31分38秒
*****************************************************************/
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
# include 
using namespace std;


const int debug = 1;
const int size  = 100000 + 10; 
const int INF = INT_MAX>>1;
const int MOD = 9973;
typedef long long ll;

char str[size];
int red[size];
int QuickPowerMod(int b,int n,int MOD){
    int ret = 1;
    while (n>0){
        if (n&1){
            ret = ret*b%MOD;
        }
        b = b*b%MOD;
        n>>=1;
    }
    return ret;
}
int main()
{
    std::ios::sync_with_stdio(false);cin.tie(0);
    int i,j;
    red[0] = 1;
    int n;
    while (cin >> n){
        cin >> (str+1);    
        int len = strlen(str+1);
        for (i=1;i<1+len;i++){
            red[i] = (red[i-1]*(str[i] - 28))%MOD;
        }
        int a,b;
        while (n--){
            cin >> a >> b;
            cout << red[b]*QuickPowerMod(red[a-1],MOD-2,MOD)%MOD << endl;
        }
    }
    return 0;
}


你可能感兴趣的:(2016百度之星资格赛,数论,逆元,费马小定理)