U432972 f'(x)

· · 个人记录

U432972 f'(x) 题解

思路

直接给公式f'(x)=na_nx^{n-1}+(n-1)a_{n-1}x^{n-2}+...+2a_2x+a_1

AC代码

// Problem: U432972 f'(x)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/U432972
// Memory Limit: 512MB
// Time Limit: 5000ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll a[100010];
ll flag=false; //为true则至少有1项,false则无项输出0
int main()
{
    cin>>n;
    for(ll i=1;i<=n+1;++i)
    {
        cin>>a[i];
    }
    cout<<"f'(x)=";
    for(ll i=1;i<=n;++i)
    {
        ll coe=a[i]*(n-i+1); //本项系数
        ll ind=n-i; //本项指数
        if(coe==0)
        {
            continue;
        }
        if(flag)
        {
            cout<<'+';
        }
        flag=true;
        if(coe!=1)
        {
            cout<<coe;
            if(ind!=0)
            {
                cout<<'*';
            }
        }
        if(coe==1&&ind==0)
        {
            cout<<coe;
        }
        if(ind==1)
        {
            cout<<'x';
        }
        if(ind!=0&&ind!=1)
        {
            cout<<"x^"<<ind;
        }
    }
    if(!flag)
    {
        cout<<0;
    }
    return 0;
}