zoukankan      html  css  js  c++  java
  • 【数论】UVa 10586

    Problem F: Polynomial Remains

    Given the polynomial

          a(x) = an xn + ... + a1 x + a0,

    compute the remainder r(x) when a(x) is divided by xk+1.

    The input consists of a number of cases. The first line of each case specifies the two integers n and k (0 ≤ n, k ≤ 10000). The next n+1 integers give the coefficients of a(x), starting from a0 and ending with an. The input is terminated if n = k = -1.

    For each case, output the coefficients of the remainder on one line, starting from the constant coefficient r0. If the remainder is 0, print only the constant coefficient. Otherwise, print only the first d+1 coefficients for a remainder of degree d. Separate the coefficients by a single space.

    You may assume that the coefficients of the remainder can be represented by 32-bit integers.

    Sample Input

    5 2
    6 3 3 2 0 1
    5 2
    0 0 3 2 0 1
    4 1
    1 4 1 1 1
    6 3
    2 3 -3 4 1 0 1
    1 0
    5 1
    0 0
    7
    3 5
    1 2 3 4
    -1 -1
    

    Sample Output

    3 2
    -3 -1
    -2
    -1 2 -3
    0
    0
    1 2 3 4

    题意已标黄,即求出一元n次多项式除以x^k+1后的余数,若余数为0,则输出0;否则,从0次幂系数r0开始输出最后一个不为0的系数。
    按照除法的模拟过程模拟即可。简单说一下模拟过程:

    比如
    5 2
    6 3 3 2 0 1
    多项式除以x^k+1后,x大于等于k的幂的系数均变为0。比如若使x^5系数变为0,则需要(x^2+1)*(6*x^3) = 6*x^5 + 6*x^3;这样就可以将原多项式的5次幂项减掉,顺带着,3次幂项系数会减6;由此规律,每抵消掉一个x次幂,其x-2次幂的系数会相应减少x次幂的系数。这样一直到使x的k次幂系数为0就好了。这样代码就很好写了。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cmath>
     6 using namespace std;
     7 const int N = 10005;
     8 int n, k, c[N], i;
     9 int main() {
    10     while (~scanf("%d%d", &n, &k) && n != -1 || k != -1)
    11     {
    12         for (i = 0; i <= n; i++)
    13             scanf("%d", &c[i]);
    14         for (i = n; i >= k; i--)
    15         {
    16             c[i - k] -= c[i];
    17             c[i] = 0;
    18         }
    19         i = n;
    20         while (c[i] == 0 && i >= 0) i--;
    21         if (i == -1) printf("0
    ");
    22         else {
    23             printf("%d", c[0]);
    24             for (int j = 1; j <= i; j++)
    25                 printf(" %d", c[j]);
    26             printf("
    ");
    27         }
    28     }
    29     return 0;
    30 }



  • 相关阅读:
    python数据类型--字符串
    Cannot open include file: 'afxcontrolbars.h': No such file or directory
    关于找不到tbb_debug.dll解决办法
    Android fill_parent、wrap_content和match_parent的区别
    多个摄像头同步工作【转】
    如何查找openCV函数源代码[转]
    Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)[转]
    CvCaptureFromCam
    OpenCV与相机的关系[转]
    10亿以内和987654互质正整数的和[转自深圳-冒泡]
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4338026.html
Copyright © 2011-2022 走看看