MT3041 多项式变换求值

简介: MT3041 多项式变换求值

c9db9d3b0f5948af97b21b733f538009.jpg

b0429a19f74445af9627b5788f3d1958.jpg


注意点:


1.使用单调栈

2.用ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);避免超时

3.此题除了ans最好不要用long long,如果a[]和b[]都是long long 类型,可能会超内存

4.ans = (ans % p + p) % p;防止负数

5.使用秦九韶算法计算指数而不是用pow(),它通过迭代的方式计算多项式的值,避免了直接计算多项式的复杂度。


代码:


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e6 + 10;
const int p = 99887765;
int n, x;
int a[N]; // 存ai
int b[N]; // 存ai之后第一个小于ai的系数aj
// 单调栈
stack<int> s; // 存位置
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> x;
    for (int i = 1; i <= n + 1; i++)
    {
        cin >> a[i]; // n,n-1...3,2,1,0次方
        while (!s.empty() && a[i] < a[s.top()])
        {
            b[s.top()] = a[i]; // 存值
            s.pop();
        }
        s.push(i);
    }
    ll ans = 0;
    for (int i = 1; i <= n + 1; i++)
    {
        ans = ans * x + b[i];    // 迭代的方式计算多项式的值
        ans = (ans % p + p) % p; // 防止负数
    }
    cout << ans;
    return 0;
}


相关文章
|
1月前
|
存储 Serverless C语言
『C/C++』Eg3:多项式求值
『C/C++』Eg3:多项式求值
|
11月前
一元函数微分学中导数--定义--意义--基本公式--运算法则
一元函数微分学中导数--定义--意义--基本公式--运算法则
罗尔(Rolle)、拉格朗日(Lagrange)和柯西(Cauchy)三大微分中值定理的定义
罗尔(Rolle)、拉格朗日(Lagrange)和柯西(Cauchy)三大微分中值定理的定义
罗尔(Rolle)、拉格朗日(Lagrange)和柯西(Cauchy)三大微分中值定理的定义
|
机器学习/深度学习 Python
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
69 0
|
算法
多项式运算专题
多项式运算专题
116 0
多项式运算专题
07:计算多项式的值
07:计算多项式的值
240 0
|
存储
【CCCC】L2-018 多项式A除以B (25分),多项式除法
【CCCC】L2-018 多项式A除以B (25分),多项式除法
140 0