FZU 2215 Simple Polynomial Problem (多项式乘法 栈)

Problem 2215 Simple Polynomial Problem

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

You are given an polynomial of x consisting of only addition marks, multiplication marks, brackets, single digit numbers, and of course the letter x. For example, a valid polynomial would be: (1+x)*(1+x*x+x+5)+1*x*x.

You are required to write the polynomial into it‘s minimal form by combining the equal terms.

For example, (1+x)*(1+x) would be written as x^2+2*x+1.

Also, (1+x)*(1+x*x+x+5)+1*x*x would be written as x^3+3*x^2+7*x+6.

Input

The first line contains an integer T, meaning the number of the cases. 1 <= T <= 50.

For each test case, there will be one polynomial which it‘s length would be not larger than 1000.

It is guaranteed that every input polynomial is valid, and every number would be a single digit.

Output

For each polynomial, output it‘s minimal form. If the polynomial‘s minimal form has n terms, output n space-separated integers.

You only have to output each term‘s corresponding constant (from highest to lowest). In case the constant gets too big, output the remainder of dividing the answer by 1000000007 (1e9 + 7).

Sample Input

4

1*(1+1*(1+1))

(1+x)*(1+x)

x*((1+x)*(1+x*x+x+5)+1*x*x)

(x*x*x*0+x*2)*x

Sample Output

3

1 2 1

1 3 7 6 0

2 0 0

Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2215

题目大意:求表达式的结果的多项式的系数

题目分析:直接用栈模拟,记录每个多项式的最大幂指数,乘法的时候一位一位乘然后相加

import java.util.Scanner;
import java.util.Stack;

/**
 * Created by tcgogogo on 16/8/20.
 */

public class Main {

    static class Polynomial {
        public int num;
        public long coef[] = new long[1005];

        public static Polynomial add(Polynomial a, Polynomial b) {
            Polynomial ans = new Polynomial();
            int Max = Math.max(a.num, b.num);
            for(int i = 0; i <= Max; i++) {
                ans.coef[i] = a.coef[i] + b.coef[i];
                ans.coef[i] %= 1000000007;
            }
            ans.num = Max;
            return ans;
        }

        public static Polynomial mul(Polynomial a, Polynomial b) {
            Polynomial ans = new Polynomial();
            if(a.coef[a.num] == 0 || b.coef[b.num] == 0) {
                return ans;
            }
            Polynomial tmp = new Polynomial();
            for(int i = 0; i <= a.num; i++) {
                if(i > 0) {
                    for(int j = b.num + 1; j >= 1; j--) {
                        b.coef[j] = b.coef[j - 1];
                    }
                    b.coef[0] = 0;
                    b.num ++;
                }
                tmp.num = b.num;
                for(int k = 0; k < b.coef.length; k++) {
                    tmp.coef[k] = b.coef[k];
                }
                for(int j = 0; j <= b.num; j++) {
                    tmp.coef[j] *= a.coef[i];
                    tmp.coef[j] %= 1000000007;
                }
                ans = add(ans, tmp);
            }
            return ans;
        }
    }
    public static Stack<Polynomial> stkPolynomial = new Stack<Polynomial>();
    public static Stack<Character> stkOperator = new Stack<Character>();

    public static void Calculate(char operator) {
        if(operator == '+' || operator == '*') {
            stkOperator.pop();
            Polynomial a = stkPolynomial.peek();
            stkPolynomial.pop();
            Polynomial b = stkPolynomial.peek();
            stkPolynomial.pop();
            if (operator == '+') {
                stkPolynomial.push(Polynomial.add(a, b));
            } else {
                stkPolynomial.push(Polynomial.mul(a, b));
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int ca = 1; ca <= T; ca ++) {
            stkPolynomial.clear();
            stkOperator.clear();
            String str = "";
            str = in.next();
            int len = str.length();
            for(int i = 0; i < len; i++) {
                if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                    Polynomial tmp = new Polynomial();
                    tmp.num = 0;
                    tmp.coef[0] = str.charAt(i) - '0';
                    stkPolynomial.push(tmp);
                }
                else if(str.charAt(i) == 'x') {
                    Polynomial tmp = new Polynomial();
                    tmp.num = 1;
                    tmp.coef[1] = 1;
                    stkPolynomial.push(tmp);
                }
                else if(str.charAt(i) == '(') {
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == '*') {
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == '+') {
                    while(!stkOperator.empty()) {
                        if(stkOperator.peek() == '*') {
                            Calculate('*');
                        }
                        else {
                            break;
                        }
                    }
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == ')') {
                    while(!stkOperator.empty()) {
                        if(stkOperator.peek() == '+') {
                            Calculate('+');
                        }
                        else if(stkOperator.peek() == '*') {
                            Calculate('*');
                        }
                        else if(stkOperator.peek() == '(') {
                            stkOperator.pop();
                            break;
                        }
                    }
                }
            }
            while(!stkOperator.empty()) {
                if(stkOperator.peek() == '+') {
                    Calculate('+');
                }
                else if(stkOperator.peek() == '*') {
                    Calculate('*');
                }
            }
            Polynomial ans = stkPolynomial.peek();
            for(int i = ans.num; i >= 0; i--) {
                if(i == 0) {
                    System.out.println(ans.coef[i]);
                }
                else {
                    System.out.print(ans.coef[i] + " ");
                }
            }
        }
    }
}
时间: 2024-10-07 18:04:15

FZU 2215 Simple Polynomial Problem (多项式乘法 栈)的相关文章

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

矩阵十题【八】 HDU 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: If x < 10   ,则  f(x) = x. If x >= 10 ,则  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题

洛谷.3803.[模板]多项式乘法(FFT)

题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. #include <cmath> #include <cctype> #include <cstdio> #include <algorithm> #define gc() getchar() const int N=1e6+5; const double PI=acos(-1); int n,m; struct Complex { double

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

[UOJ 0034] 多项式乘法

#34. 多项式乘法 统计 描述 提交 自定义测试 这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,分别表示第一个多项式的 00 到 nn 次项前的系数. 第三行 m+1m+1 个整数,分别表示第一个多项式的 00 到 mm 次项前的系数. 输出格式 一行 n+m+1n+m+1 个整数,分别表示乘起来后的多项式的 00 到 n+mn+m 次项前的系数. 样例一 input 1 2 1

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

求幂运算、多项式乘法及Horner法则的应用

一,两种不同的求幂运算 求解x^n(x 的 n 次方) ①使用递归,代码如下: 1 private static long pow(int x, int n){ 2 if(n == 0) 3 return 1; 4 if(n == 1) 5 return x; 6 if(n % 2 == 0) 7 return pow(x * x, n / 2); 8 else 9 return pow(x * x, n / 2) * x; 10 } 分析: 每次递归,使得问题的规模减半.2到6行操作的复杂度为

hdu1757-- A Simple Math Problem(矩阵快速幂优化)

A Simple Math Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2