bzoj1943: [Ceoi2007]Nasty Calculations

传送门
我们发现这个式子可以被化简为一个一次多项式。
然后我们大力算出多项式
每一次大力计算即可
当然,我们显然需要取模偷懒233.

#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100005
using namespace std;
char s[N];
int mo,n,len,top;
int a[N],ans[40]; 
int cg1(char ch){
    return ch<='9'?ch-'0':ch-'A'+10;
}
char cg2(int x){
    return x>9?'A'+x-10:'0'+x;
}
int solve(int tmp){
    top=0;
    for (int i=0;i<len;i++){
        for (;s[i]!=' '&&i<len;i++);
        int j=i-1;
        if (s[j]=='x'||(s[j]>='0'&&s[j]<='9')||(s[j]>='A'&&s[j]<='Z')){
            if (s[j]=='x') a[++top]=tmp;
            else a[++top]=cg1(s[j]);
        }
        else{
            if (s[j]=='+') a[top-1]=(a[top-1]+a[top])%mo;
            if (s[j]=='-') a[top-1]=(a[top-1]-a[top]+mo)%mo;
            if (s[j]=='*') a[top-1]=(a[top-1]*a[top])%mo;
            top--;
        }
    }
    return a[1];
}
int main(){
    scanf("%d%d\n",&mo,&n);
    cin.getline(s,N);
    len=strlen(s);
    for (int i=0;i<mo;i++)
        ans[i]=solve(i);
    for (int i=1;i<=n;i++){
        scanf("%s",s+1);
        len=strlen(s+1);
        printf("%c\n",cg2(ans[cg1(s[len])]));   
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值