CCFCSP第31次认证第三题梯度求解
先把常数代入,转换为一元多次多项式,求导后计算当前点的导数值
C++ 代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;
const int N = 110 , MOD = 1e9+7;
//typedef long long LL;
int n,m;
int value[N];
string s;
unordered_map<int,int> simplity(string s,int id){
stringstream str(s);
stack<unordered_map<int,int>> stk;//表示次数和它的系数
while (str)
{
string p;
str>>p;
if(p[0]=='x'){
int t=stoi(p.substr(1));
if(id==t){
unordered_map<int,int> state;
state[1]=1;
stk.push(state);
}
else{
unordered_map<int,int> state;
state[0]=value[t];
stk.push(state);
}
}
else if(p=="+"){
auto x2=stk.top();
stk.pop();
auto x1=stk.top();
stk.pop();
unordered_map<int,int> state;
for(auto i:x1){
state[i.first]=i.second;
}
for(auto i:x2){
state[i.first]=state[i.first]+i.second;
}
stk.push(state);
}
else if(p=="-"){
auto x2=stk.top();
stk.pop();
auto x1=stk.top();
stk.pop();
unordered_map<int,int> state;
for(auto i:x1){
state[i.first]=i.second;
}
for(auto i:x2){
state[i.first]=state[i.first]-i.second;
}
stk.push(state);
}
else if(p=="*"){
auto x2=stk.top();
stk.pop();
auto x1=stk.top();
stk.pop();
unordered_map<int,int> state;
for(auto i:x1){
for(auto j:x2){
state[i.first+j.first]+=(i.second*j.second)%MOD;
}
}
stk.push(state);
}
else if(p!=""){
//cout<<"p:"<<p<<endl;
int t=stoi(p);
unordered_map<int,int> state;
state[0]=t;
stk.push(state);
}
}
//cout<<stk.size()<<endl;
return stk.top();
}
unordered_map<int,int> convert(unordered_map<int,int> state){
unordered_map<int,int> g;
for(int i=0;i<70;i++){
g[i]=state[i+1]*(i+1)%MOD;
}
return g;
}
int compute(unordered_map<int,int> state,int x){
int r=1;
int res=state[0];
for(int i=1;i<70;i++){
r=r*x%MOD;
res=(res+r*state[i])%MOD;
}
return (res+MOD)%MOD;
}
signed main(){
cin>>n>>m;
getchar();
getline(cin,s);
//cout<<s<<endl;
while(m--){
int id;
cin>>id;
for(int i=1;i<=n;i++){
cin>>value[i];
}
//string str=s;
unordered_map<int,int> state=simplity(s,id);
state=convert(state);
cout<<compute(state,value[id])<<endl;
}
return 0;
}