csp202309-3梯度求解

 为了准备第一次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;
    }

}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值