Sicily 1040. Polly Nomials
来源:互联网 发布:windows7软件字体大小 编辑:程序博客网 时间:2024/06/03 07:22
1040. Polly Nomials
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The Avian Computation Mission of the International Ornithologists Union is dedicated to the study of intelligence in birds, and specifically the study of computational ability. One of the most promising projects so far is the "Polly Nomial" project on parrot intelligence, run by Dr. Albert B. Tross and his assistants, Clifford Swallow and Perry Keet. In the ACM, parrots are trained to carry out simple polynomial computations involving integers, variables, and simple arithmetic operators.
When shown a formula consisting of a polynomial with non-negative integer coefficients and one variable x, each parrot uses a special beak-operated PDA, or "Parrot Digital Assistant," to tap out a sequence of operations for computing the polynomial. The PDA operates much like a calculator. It has keys marked with the following symbols: the digits from 0 through 9, the symbol 'x', and the operators '+', '*', and '='. (The x key is internally associated with an integer constant by Al B. Tross for testing purposes, but the parrot sees only the 'x'.)
For instance, if the parrot were presented with the polynomial
x^3 + x + 11
the parrot might tap the following sequence of symbols:
x, *, x, *, x, +, x, +, 1, 1, =
The PDA has no extra memory, so each * or + operation is applied to the previous contents of the
display and whatever succeeding operand is entered. If the polynomial had been
x^3 + 2x^2 + 11
then the parrot would not have been able to "save" the value of x^3 while calculating the value of 2x^2. Instead, a different order of operations would be needed, for instance:
x, +, 2, *, x, *, x, +, 1, 1, =
The cost of a calculation is the number of key presses. The cost of computing x^3+x+11 in the example above is 11 (four presses of the x key, two presses of '*', two presses of '+', two presses of the digit '1', and the '=' key). It so happens that this is the minimal cost for this particular expression using the PDA.
You are to write a program that finds the least costly way for a parrot to compute a number of polynomial expressions. Because parrots are, after all, just bird-brains, they are intimidated by polynomials whose high-order coefficient is any value except 1, so this condition is always imposed.
Input
Input consists of a sequence of lines, each containing a polynomial and an x value. Each polynomial anx^n+an-1xn^-1+ . . . +a0 is represented by its degree followed by the non-negative coefficients an, . . ., a0 of decreasing powers of x, where an is always 1. Degrees are between 1 and 100. The coefficients are followed on the same line by an integer value for the variable x, which is always either 1 or -1. The input is terminated by a single line containing the values 0 0.
Output
For each polynomial, print the polynomial number followed by the value of the polynomial at the given integer value x and the minimum cost of computing the polynomial; imitate the formatting in the sample output.
Sample Input
3 1 0 1 11 13 1 0 2 11 -10 0
Sample Output
Polynomial 1: 13 11Polynomial 2: 8 11
// Problem#: 1040// Submission#: 3283451// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>using namespace std;const int MAX_N = 1005;int N;int men[MAX_N][MAX_N];int women[MAX_N][MAX_N];int point[MAX_N][MAX_N];int menChoose[MAX_N];vector<int> choosed[MAX_N];int getDigits(int n) { int d = 0; while (n) { d++; n /= 10; } return d;}int main() { std::ios::sync_with_stdio(false); int maxPower, an, x, last; int c[105]; int counter = 0; while (1) { counter++; cin >> maxPower >> an; if (maxPower == 0 && an == 0) break; for (int i = maxPower - 1; i >= 1; i--) { cin >> c[i]; } cin >> last >> x; int ans = x; int cost = 0; for (int i = maxPower - 1; i >= 1; i--) { if (c[i]) { cost += 2; cost += getDigits(c[i]); } else { cost++; } ans = (ans + c[i]) * x; } ans += last; if (last) { cost += maxPower + 2 + getDigits(last); } else { cost += maxPower + 1; } if (maxPower) { cout << "Polynomial " << counter << ": " << ans << " " << cost << endl; } else { cout << "Polynomial " << counter << ": " << 1 << " " << 2 << endl; } } return 0;}
- sicily--1040. Polly Nomials
- Sicily 1040. Polly Nomials
- UVa 498 - Polly the Polynomial
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- 弹性和瞬态故障处理库Polly介绍
- 弹性和瞬态故障处理库Polly之重试策略
- Polly 弹性和瞬态故障处理库 介绍
- Sicily 1027
- sicily 1007
- sicily 1795
- sicily 1036
- sicily 1419
- sicily 1889
- sicily 1684
- Cannot upgrade/downgrade to Dynamic Web Module 2.3 facet
- ios view的frame和bounds之区别(位置和大小)
- HDOJ 5184 Brackets 卡特兰数扩展
- Android ORM框架之 ORMLite
- (三)Aws引用S3Client时候总是出现not found
- Sicily 1040. Polly Nomials
- HashMap排序
- iOS 中监听观察通知的几种方法
- Sicily 1224. 速配游戏
- Pascal's Triangle
- Sicily 1777. Mix order traversal
- 算法训练 Torry的困惑(基本型) 【打表】
- 2015年天勤考研机试模拟赛 A 判断三角形
- Qt元对象