为了准备第一次csp练的,但是只拿了90分(也不要脸地发一下,应该思路没啥大问题)
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
using ll = long long;
int main(){
int n;
int m;
string str1;
string str;
string tmp;
getline(cin,str1);
int i=0;
while(str1[i]!=' '){
i++;
}
n= stoi(str1.substr(0,i));
m= stoi(str1.substr(i+1,str1.size()-i-1));
getline(cin,str);
vector<string> v;
stringstream ss(str);
while(getline(ss,tmp,' ')){
v.push_back(tmp);
}
stack<map<ll,ll>> s;
for(i=0;i<m;i++){
int index;
ll parameter[101];
cin>>index;
for(int j=1;j<=n;j++){
cin>>parameter[j];
}
for(auto it:v){
if(it[0]=='x'){
map<ll,ll> m1;
int temp=stoi(it.substr(1,it.size()-1));
if(temp==index){
m1[1]=1;
}
else{
m1[0]=parameter[temp]%mod;
}
s.push(m1);
}
else if(it.size()==1&&!isdigit(it[0])){
if(it=="+"){
map<ll,ll> m1=s.top();s.pop();
map<ll,ll> m2=s.top();s.pop();
map<ll,ll> res;
for(auto item:m1){
res[item.first]=item.second;
}
for(auto item:m2){
res[item.first]+=item.second;
res[item.first]%=mod;
}
s.push(res);
}
if(it=="-"){
map<ll,ll> m1=s.top();s.pop();
map<ll,ll> m2=s.top();s.pop();
map<ll,ll> res;
for(auto item:m2){
res[item.first]=item.second;
}
for(auto item:m1){
res[item.first]-=item.second;
res[item.first]%=mod;
}
s.push(res);
}
if(it=="*"){
map<ll,ll> m1=s.top();s.pop();
map<ll,ll> m2=s.top();s.pop();
map<ll,ll> res;
for(auto item1:m1){
for(auto item2:m2){
res[item1.first+item2.first]=(item1.second*item2.second)%mod;
}
}
s.push(res);
}
}
else {
int dight=stoi(it);
map<ll,ll> res;
res[0]=dight%mod;
s.push(res);
}
}
ll result=0;
map <ll,ll> res=s.top();
for(auto item:res){
if(item.first!=0)
result+=item.second*item.first*pow(parameter[index],item.first-1);
result%=mod;
}
if(result<0)
result+=mod;
cout<<result<<endl;
}
}