UVA10268




Sample Input

7
1 -1
2
1 1 1

Sample Output

1
5


大意:給多項式公式 求微分的結果

解法: 
horner's rule 開到long

EX: x^3 + x^2 + x + 1

f(x)= ((x+1)x)x+1

f'(x)= (3x+2)x+1




import java.util.Scanner;

public class UVA10268 {

 public static void main(String args[]) {

  Scanner sc = new Scanner(System.in);

  while (sc.hasNext()) {

   long X = Long.parseLong(sc.nextLine());

   long sum = 0;

   long last = 0;

   String s[] = sc.nextLine().split(" ");

   long number[] = new long[s.length];
   boolean number_box[] = new boolean[s.length];

   for (int i = 0; i < s.length; i++) {

    boolean check = false;

    for (int j = 0; j < s[i].length(); j++) {

     if (s[i].charAt(j) == '-' && '9' - s[i].charAt(j + 1) >= 0
       && s[i].charAt(j + 1) - '0' <= 9) {
      check = true;

     }

     else if ('9' - s[i].charAt(j) >= 0
       && s[i].charAt(j) - '0' <= 9
       && s[i].charAt(j) != '-') {
      check = true;

     } else {
      check = false;
      break;
     }

    }

    if (check) {
     number_box[i] = true;
    }

    else
     continue;

   }
   int k = 0;

   for (int i = 0; i < number_box.length; i++) {

    if (number_box[i] == true) {

     last++;

     number[k] = Long.parseLong(s[i]);

     k++;
    }

   }

   long j = last;

   int i;

   for (i = 0; i < j - 2; i++) {

    last--;

    sum = (sum + (last * number[i])) * X;

   }

   if (last < 2)
    sum = 0;

   else
    sum += number[i];

   System.out.println(sum);

  }

  sc.close();
 }

}