7-141 Standard Form of Polynomial

题目

给定多项式 \(P(x)=(x-r_1)\cdot (x-r_2)\cdots (x-r_n)\),要求 \(P(x)=a_n x^n+a_{n-1} x^{n-1}\cdots a_0\) 的每一项系数 \(a\)

\(n\le 10\),中间所有运算不会超过 int 范围。


解题报告

我一开始看错题了,以为是叫我因式分解。

那份看错了的题面的做法就放在 我的 blog 里了,虽然只有口胡。

这道题,你直接暴力枚举每一个 \((x-r_i)\) 这一项,取的是 \(x\) 还是 \(-r_i\),然后记录一下 \(x\) 的个数,将答案贡献上去即可。


Code

#include <stdio.h>
#define LL long long
using namespace std;
const int Rea=1e5+3;
struct Rin
{
    char c;
    inline char gc()
    {
        static char rea[Rea];
        static char *head,*tail;
        return head==tail&&(tail=(head=rea)+fread(rea,1,Rea,stdin),head==tail)?EOF:*head++;
    }
    inline Rin&operator >>(int &x)
    {
        x=0;
        bool tag=false;
        for(c=gc();c>'9'||c<'0';c=gc())if(c=='-'){c=gc();tag=true;break;}
        for(;c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+(c^'0');
        if(tag)x=-x;
        return *this;
    }
}rin;

const int N=13;
int n;
int a[N];
int r[N];

inline void dfs(int x,int s,int cutt)
{
    if(x>n){a[cutt]+=s;return;}
    dfs(x+1,s*(-r[x]),cutt-1);dfs(x+1,s,cutt);
    return;
}
int main()
{
    rin>>n;for(int i=1;i<=n;i++)rin>>r[i];dfs(1,1,n);
    for(int i=n-1;i>0;i--)printf("%d ",a[i]);printf("%d",a[0]);
    return 0;
}

\[\texttt{El Psy Congroo} \]

posted @ 2021-02-27 10:56  zjjws  阅读(166)  评论(0)    收藏  举报