一、题目描述

Given a polynomial and the value of the variable x, you task is to calculate the value of the polynomial.

二、输入

The first line of the input is a positive integer T. T is the number of test cases followed.

In each test case, the first line contains two integer n and x (0<=n<=50, 0<=x<=10000). The second line contains n+1 integers, representing the coefficients of a polynomial from power degree N down to power degree 0, each integer is no less than 0 and no more than 10000.

三、输出

The output of each test case should consist one line, containing the result of the polynomial.

例如:

输入:

1

2 5

1 2 3

输出:

38

四、解题思路

题意:这道题要求的是多项式值。如果只是简单的求多项式的值很简单。可题目有个条件是 (0<=n<=50, 0<=x<=10000),这样求出来的数已经超过了long long类型。

开始不知道怎么处理这个大数问题,在这里卡住了。后来同学说使用数组处理。以前没接触过使用输出保存大数的问题,当时也上不了网,于是自己思考一下,使用一个数组n表示n位十进制。每位表示十进制中的一位数。大数+(int类型)常数;大数*(int类型)常数。

1、多项式计算方法

记得高中学过求多项式值得方法能较少计算量:

如多项式:1*5^2+2*5^1+3*5^0

2、数组表示整数

例如439保存在数组bigInteger[3];bigInteger[2]=4;bigInteger[1]=3;bigInteger[0]=9;

1)数组表示的整数加上一个常数num

例如加上35。

先从最低位开始,例如bigInteger[0]+35=43;bigInteger[0]=43%10;进位数carryInt=43/10=4;

向高位逐步计算

bigInteger[1]+4=7,bigInteger[1]=7%10,carryInt=7/10。

当出现carryInt为0是停止计算,否则不断想高位计算。

计算的结果是:

bigInteger[2]=4;bigInteger[1]=7;bigInteger[0]=4;

2)数组表示的整数乘以一个常数num

例如bigInteger[2]=4;bigInteger[1]=7;bigInteger[0]=4;乘以4。

也是从最低位开始bigInteger[0]*4=16,bigInteger[0]=16%10=6;

进位数carryInt=16/10=1;

继续到下一位(高位):bigInteger[1]*4=28,28+carryInt=29,bigInteger[1]=29%10=9,

carryInt=29/10=2;

继续下一位:bigInteger[2]*4=16,16+carryInt=18,bigInteger[2]=18%10=8,

carryInt=18/10=1;

继续下一位:

bigInteger[3]开始至为0,所以bigInteger[3]=bigInteger[3]+carryInt=1。

最后计算结果为:bigInteger[3]=1,bigInteger[2]=8,bigInteger[1]=9,bigInteger[0]=6

五、代码

#include<iostream>

using namespace std;

const int BIT_NUM = 200;    //支持大数的最大位数(200位)

int main()
{
int times;
cin >> times; while(times--)
{
int bigInteger[BIT_NUM] = {0}; //用数组保存一个大数
int n, x, argue; //n-多项式的最高次数,x-位置数x的值,argue-每一项的系数
long long sum = 0; //多项式的结果
cin >> n >> x;
for(int k = 0; k < (n + 1); k++) //计算多项式,这里使用了一个公式例如1*5^2+2*5^1+3*5^0=38 可以写成(1*5+2)*5+3=38
{
cin >> argue;
int carryInt = 0; //进位(从低位算起,例如各位数是7乘以一个常数8,那么结果各位数是6,向高位数进5(carryInt))
for(int i = BIT_NUM; i > 0; i--) //一个大数乘以一个常数,从各位开始逐个与常数相乘,满十向高位进
{
bigInteger[i-1] = bigInteger[i-1] * x + carryInt;
carryInt = bigInteger[i-1] / 10;
bigInteger[i-1] %= 10;
} //大数加上一个常数
int addCarry = 0;
bigInteger[BIT_NUM - 1] += argue; //大数加上一个常数argue,先个位数加
for(int i = BIT_NUM; i > 0; i--)
{
bigInteger[i-1] = bigInteger[i-1] + addCarry;
addCarry = bigInteger[i-1] / 10; //addCarry保存进制数
bigInteger[i-1] %= 10;
if(addCarry == 0) break; //如果出现不需要向高位数进的退出
}
} int flag = false;
for(int i = 0; i < BIT_NUM; i++) //从高位第一个非0开始输出大数
{
if(bigInteger[i] != 0) {flag = true;}
if(flag) {cout << bigInteger[i];}
}
cout << endl; }
return 0;
}

<Sicily>Polynomial的更多相关文章

  1. Polynomial Library in OpenCascade

    Polynomial Library in OpenCascade eryar@163.com 摘要Abstract:分析幂基曲线即多项式曲线在OpenCascade中的计算方法,以及利用OpenSc ...

  2. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  3. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  4. 周赛-Integration of Polynomial 分类: 比赛 2015-08-02 08:40 10人阅读 评论(0) 收藏

    Integration of Polynomial Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...

  5. FZU 2215 Simple Polynomial Problem(简单多项式问题)

    Description 题目描述 You are given an polynomial of x consisting of only addition marks, multiplication ...

  6. Project Euler 101 :Optimum polynomial 最优多项式

    Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...

  7. 大数求模 sicily 1020

        Search

  8. 【数论】UVa 10586 - Polynomial Remains

    Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...

  9. Sicily 1510欢迎提出优化方案

    这道题我觉得是除1000(A-B)外最简单的题了……不过还是提出一个小问题:在本机用gcc编译的时候我没包括string.h头文件,通过编译,为什么在sicily上却编译失败? 1510. Mispe ...

随机推荐

  1. vue Render scopedSlots

    render 中 slot 的一般默认使用方式如下: this.$slots.default 对用 template的<slot>的使用没有name . 想使用多个slot 的话.需要对s ...

  2. composer是什么

    composer是什么 Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你声明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 &q ...

  3. lucene简单使用demo

    测试结构目录: 1.索引库.分词器 Configuration.java package com.test.www.web.lucene; import java.io.File; import or ...

  4. 752. [BJOI2006] 狼抓兔子

    ★★★   输入文件:bjrabbit.in   输出文件:bjrabbit.out   简单对比时间限制:1 s   内存限制:162 MB Description   Source: Beijin ...

  5. 简易的CSS下拉菜单 - 1

    <!DOCTYPE html> <html> <head> <style> body{ margin:0; } .dropmenu{ backgroun ...

  6. 新疆大学OJ(ACM) 1047: string 字符串排序

    1047: string 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n个字符串字符串n<=50000,把所有字符串串起来,得到一个字典序最小的字符串. 输入 输入第一行是一 ...

  7. JQuery插件的写法 (转:太棒啦!)

    JQuery插件写法的总结 最近Web应用程序中越来越多地用到了JQuery等Web前端技术.这些技术框架有效地改善了用户的操作体验,同时也提高了开发人员构造丰富客户 端UI的效率.JQuery本身提 ...

  8. IIS支持10万个同时请求的设置

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...

  9. SPA SEO SSR三者有什么区别

    SPA通俗的说就是单页面应用(single page application) 优点 页面之间的切换非常快 一定程度减少了后端服务器的压力 后端程序只需要提供api,不需要客户端到底是web端还是手机 ...

  10. 总结Ajax的一些细节

    Ajax的总结 主要从Ajax是什么?可以用来干什么?基本要素,优缺点,执行过程,跨域的解决方案等几方面来解释. Ajax是什么? Ajax主要用来实现客户端与服务器端的异步通信效果,实现页面的局部刷 ...