P2312 解方程
P2312 解方程
我借鉴了一下某位不愿透露姓名的丁大佬的代码,他讲了一遍。。。但是我并没有听懂
这个题目呢我们用到了秦九韶算法
把一个n次多项式
![](https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D263/sign=67dc3068868ba61edbeecf29723597cc/38dbb6fd5266d016dfcda88a932bd40734fa35bf.jpg)
改写成如下形式:
![](https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D30/sign=baca3fcfaf345982c18ae3920df4dcd3/c2cec3fdfc039245fb9f4ab48594a4c27d1e2566.jpg)
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D229/sign=7f42dc2cdb33c895a27e9f79e8127397/377adab44aed2e73fbb22d978501a18b87d6fa36.jpg)
![](https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D295/sign=3ca7a54c9d3df8dca23d8898f81072bf/d833c895d143ad4b9d7bed7b80025aafa40f0637.jpg)
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D331/sign=56d91e811bd8bc3ec20800c9b38aa6c8/dcc451da81cb39db8ca5ccedd2160924ab183027.jpg)
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D16/sign=cbfaaba1c45c1038207ecac4b311c96a/71cf3bc79f3df8dcf3aa242acf11728b461028f1.jpg)
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D303/sign=a29ff572c295d143de76e22340f08296/8601a18b87d6277f7e172b4922381f30e924fcbe.jpg)
求多项式的值时,首先计算最内层括号内一次多项式的值,即
![](https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D49/sign=d8cd0a65f21f3a295ec8d4c798253458/a50f4bfbfbedab6457d6e67bf536afc378311ee0.jpg)
![](https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D105/sign=c9994803d63f8794d7ff4c2ee71a0ead/aec379310a55b319b01829ce41a98226cefc17a3.jpg)
然后由内向外逐层计算一次多项式的值,即
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D105/sign=ec7690380d2442a7aa0ef9a5e442ad95/2f738bd4b31c8701d3ac185d257f9e2f0708ff77.jpg)
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D105/sign=e111c43ecbea15ce45eee40983013a25/55e736d12f2eb938797ad049d7628535e5dd6f72.jpg)
![](https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D16/sign=cc296271ad51f3dec7b2bd6295ee39fd/83025aafa40f4bfbaad234ef014f78f0f736180e.jpg)
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D106/sign=590632bf5b82b2b7a39f3dc407accb0a/3b292df5e0fe99254473c3eb36a85edf8db1710a.jpg)
这样,求n次多项式f(x)的值就转化为求n个一次多项式的值。
结论:对于一个n次多项式,至多做n次乘法和n次加法。
代码:
#include <cstdio> const long long Mod = (int)1e9 + 7; const int maxN = 100 + 5; const int maxM = (int)1e6 + 5; int N, M; int arr[maxN]; void Fscan(int &tmpX)
{ int Ch = getchar(), F = ' '; long long tmp = 0; while (Ch < '0' || Ch > '9')
{ F = Ch; Ch = getchar(); } while ('0' <= Ch && Ch <= '9')
{ tmp = ((tmp << 3) + (tmp << 1) + Ch - '0') % Mod; Ch = getchar(); } tmpX = (int)(F == '-' ? -tmp : tmp); } void Read()
{ scanf("%d%d", &N, &M); for (int i = 0; i <= N; ++i) Fscan(arr[i]); } int T, Que[maxM]; long long Calc(const int &X)
{ long long Ans = 0; for (int i = N; i; --i) Ans = ((Ans + (long long)arr[i]) * (long long)X) % Mod; Ans = (Ans + (long long)arr[0]) % Mod; return Ans; } void Solve()
{ for (int i = 1; i <= M; ++i) if (!Calc(i)) Que[++T] = i; } int main() { Read(); Solve(); printf("%d\n", T); for (int i = 1; i <= T; ++i) printf("%d\n", Que[i]); return 0; }