-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasd.cpp
More file actions
96 lines (90 loc) · 2.33 KB
/
Copy pathhasd.cpp
File metadata and controls
96 lines (90 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
using namespace std;
#define db double
#define ll long long int
#define m 99999989
ll bigmod(ll b, ll p)
{
if(p==0)
{
return 1;
}
if(p%2==0)
{
ll r=bigmod(b,p/2);
return (r%m*r%m)%m;
}
else
{
return (b%m*bigmod(b,p-1)%m)%m;
}
}
ll modInv(ll a)
{
return bigmod(a,m-2);
}
string s;
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
////////////////////////////////////////////////
ll tc,i,j,k,b=2,b1=3,b2=5,md,md1,md2,nxt,nxt1,nxt2;
ll hs,hs1,bh,bh1,hs2,bh2,c,sz;
cin>>tc;
while(tc--)
{
cin>>s;
hs=hs1=bh=bh1=hs2=bh2=0LL;
c=0;
sz=s.size();
for(i=0; i<sz; i++)
{
hs=(hs+((bigmod(b,i)%m)*(s[i]-'a'+1))%m)%m;
hs1=(hs1+((bigmod(b1,i)%m)*(s[i]-'a'+1))%m)%m;
hs2=(hs2+((bigmod(b2,i)%m)*(s[i]-'a'+1))%m)%m;
}
j=0;
for(i=sz-1; i>=0; i--)
{
bh=(bh+((bigmod(b,j)%m)*(s[i]-'a'+1))%m)%m;
bh1=(bh1+((bigmod(b1,j)%m)*(s[i]-'a'+1))%m)%m;
bh2=(bh2+((bigmod(b2,j)%m)*(s[i]-'a'+1))%m)%m;
j++;
}
md=modInv(b)%m;
md1=modInv(b1)%m;
md2=modInv(b2)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