首页 技术 正文
技术 2022年11月15日
0 收藏 663 点赞 4,358 浏览 1521 个字

设a为一个质数,模数为另一个质数,然后暴力算多项式的答案,如果答案相等就认为两个多项式相等。

这种hash有出错概率的题为什么还是要用hash呢?因为出错的概率实在太小了,a和模数的值取得好出题人根本没法卡。

然后贡献了2次WA,第一次因为判断数字时没判断边界,第二次因为乘法运算时爆int了!!!

hash大法好~

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 10003;
const int a = 10007;
const int p = 100007;char s[N], c[N], stf[N];
int num, now, stnum[N], topnum, sty[N], topfh;
int get(char c) {if (c == '+' || c == '-') return 1; if (c == '*') return 2; if (c == '^') return 3;}
bool fh(char c) {return c == '+' || c == '-' || c == '*' || c == '^' || c == '(' || c == ')';}
int ipow(int a, int b) {
ll s = 1;
for(int i = 1; i <= b; ++i)
s = s * a % p;
return (int) s;
}
int cal(char fh, int a, int b) {
switch (fh) {
case '+':
return (a + b) % p;
break;
case '-':
return (a - b + p) % p;
break;
case '*':
return (int) (1ll * a * b % p);
break;
case '^':
return ipow(a, b);
break;
}
}
int _() {
int len = strlen(s), tmp = 0, j = 100, k;
c[0] = ' ';
for(int i = 0; i < len; ++i)
if (s[i] != ' ') c[++tmp] = s[i];
c[tmp + 1] = ' ';
len = tmp; tmp = 1; topnum = topfh = 0;
while (tmp <= len) {
if (fh(c[tmp])) {
if (c[tmp] == '(') j += 10;
else if (c[tmp] == ')') j -= 10;
else {
k = get(c[tmp]);
while (topfh && sty[topfh] >= k + j) {
stnum[topnum - 1] = cal(stf[topfh], stnum[topnum - 1], stnum[topnum]);
--topnum; --topfh;
}
stf[++topfh] = c[tmp]; sty[topfh] = k + j;
}
++tmp;
} else {
if (c[tmp] == 'a') stnum[++topnum] = a, ++tmp;
else {
k = 0;
for(;c[tmp] >= '0' && c[tmp] <= '9' && tmp <= len; ++tmp)
k = k * 10 + c[tmp] - '0';
stnum[++topnum] = k;
}
}
}
while (topfh) {
stnum[topnum - 1] = cal(stf[topfh], stnum[topnum - 1], stnum[topnum]);
--topnum; --topfh;
}
return stnum[1];
}
int main() {
gets(s);
num = _();
int m;
scanf("%d\n", &m);
for(int i = 0; i < m; ++i) {
gets(s);
now = _();
if (now == num) putchar('A' + i);
}
puts("");
return 0;
}

神奇的hash啊,你的低错误率是多么玄学,你的可靠性那么扑朔迷离~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856