# CCFCSP第31次认证第三题梯度求解

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;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值