csp31第三题

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;

int a[102];

void solve(string str,int n){
	int num;
	cin>>num;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]=(a[i]+mod)%mod;
	}
	stack< map<ll,ll> >stk;
	istringstream istr(str);
	string out;
	while(istr>>out){
		if(out=="+"||out=="-"||out=="*"){
			auto y=stk.top();stk.pop();
			auto x=stk.top();stk.pop();
			map<ll,ll>res;
			if(out=="+"){
				for(auto c:x)res[c.first]=c.second;
				for(auto c:y)res[c.first]=(res[c.first]+c.second)%mod;
			}
			else if(out=="-"){
				for(auto c:x)res[c.first]=c.second;
				for(auto c:y)res[c.first]=(res[c.first]-c.second+mod)%mod;
			}
			else {
				for(auto c1:x){
					for(auto c2:y){
						res[c1.first+c2.first]+=c1.second*c2.second%mod;
						res[c1.first+c2.first]%=mod;
					}
				}
			}
			stk.push(res);
		}
		else if(out[0]=='x'){
			int c=atoi(out.substr(1).c_str());
			map<ll,ll>res;
			if(num==c){
				res[1]=1;
			}
			else {
				res[0]=a[c];
			}
			stk.push(res);
		}
		else {
			int c=atoi(out.c_str());
			c=(c+mod)%mod;
			map<ll,ll>res;
			res[0]=c;
			stk.push(res);
		}
	}
	auto t=stk.top();
	ll res=0;
	for(auto c:t){
		ll tmp=c.second*c.first%mod;
		for(int i=1;i<=c.first-1;i++)tmp=tmp*a[num]%mod;
		res=(res+tmp)%mod;
	}
	cout<<res<<endl;
}
int main(){
	
//	freopen("1.txt","r",stdin);
	int n,m;
	cin>>n>>m;
	getchar();
	string str;
	getline(cin,str);
	while(m--){
		solve(str,n);
	}
	return 0;
}

考察了多项式表示和求解,用map表示更省空间

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deep_Miner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值