Implementation of Java polynomial expansion

Source: Internet
Author: User
Tags pow

Problem Description:

Input: A, B, N, C, D, t values

(ax+b) ^n* (cx+d) ^t expansion

Examples:

Input: 2 3 4 2 4 5

Output: [(2x+3) ^4]*[(2x+4) ^5] is expanded as follows:

82944x^0+428544x^1+981504x^2+1307904x^3+1117504x^4+634912x^5+239872x^6+58112x^7+8192x^8+512x^9

Algorithm parsing:

A. Expand the first formula (AX+B) ^n; use Key1[n] to store the coefficients of the expanded x^n, and similarly key2[] to record the second.

B. I starting from 0 N, with Key[n]==key1[i]*key[n-i]+key[n].

C. key[] Array records the coefficients of the x^0--x^n in the result.

Code:

1  PackageXiaomiduoxiangshi;2 3 ImportJava.util.*;4 5  Public classXiaomiBishi1 {6     Static voidCengji (intAintBintNintky[]) {7          8          intA=a;intB=b;intn=N;9           for(inti=0;i<=n;i++)Ten          {  OneKy[i]= (int) Math.pow (A, i) * (int) Math.pow (b, ni); A             //System.out.println (Ky[i]); -          } -System.out.println ("Paused"); the           for(inti=0;i<=n;i++) -          { -               for(intj=0;j<i;j++) -ky[i]=ky[i]* (N-J)/(j+1); +             //System.out.println (Ky[i]); -          } +         //System.out.println ("paused"); A      } at  Public Static voidMain (string[] args) { -       -      inta,b,c,d,n,t; - String F; - String k_s[]; - Integer k[]; inScanner br=NewScanner (system.in); -F=br.nextline ();//assign the input string to F to br.close (); +K_s=f.split ("");  -A=integer.parseint (k_s[0]); theB=integer.parseint (k_s[1]); *N=integer.parseint (k_s[2]); $C=integer.parseint (k_s[3]);Panax NotoginsengD=integer.parseint (k_s[4]); -T=integer.parseint (k_s[5]); the      /* + System.out.print (a); A System.out.print (b); the System.out.print (c); + System.out.print (d); - System.out.println (n); $      */ $      int[] key1; -      int[] key2; -key1=New int[1000]; theKey2=New int[1000]; -      intkey[]=New int[10000];Wuyi Cengji (a,b,n,key1); the Cengji (c,d,t,key2); -SYSTEM.OUT.PRINTLN ("The result is:"); Wu       for(intp=0;p<=n+t;p++){ -Key[p]=0; About          for(intq=0;q<=p;q++){ $                //System.out.println ("Begin:"); -                //System.out.println (key[p]); -                //System.out.println (q); -                //System.out.println (p-q); A                //System.out.println (key1[p]); +                //System.out.println (Key2[p-q]); thekey[p]=key[p]+key1[q]*key2[p-Q]; -                 //System.out.println (key[p]); $         } the         if(p!=0) theSystem.out.print ("+"); the System.out.print (key[p]); theSystem.out.print ("x^"); - System.out.print (p); in      } the System.out.println (); the      } About}

Implementation of Java polynomial expansion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.