跳到正文

个人记录

U432972 f'(x)

Gyf117约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

U432972 f′(x)f'(x) 题解

思路

直接给公式f′(x)=nanxn−1+(n−1)an−1xn−2+...+2a2x+a1f'(x)=na_nx^{n-1}+(n-1)a_{n-1}x^{n-2}+...+2a_2x+a_1

AC代码

C++
// 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;
}

原文