# UVA 10268 498-bis ## 題目連結 [UVA 10268](https://vjudge.net/problem/UVA-10268) ### 題目內容 Looking throw the “Online Judge’s Problem Set Archive” I found a very interesting problem number 498, titled “Polly the Polynomial”. Frankly speaking, I did not solve it, but I derived from it this problem. Everything in this problem is a derivative of something from 498. In particular, 498 was “... designed to help you remember ... basic algebra skills, make the world a better place, etc., etc.”. This problem is designed to help you remember basic derivation algebra skills, increase the speed in which world becomes a better place, etc., etc. In 498 you had to evaluate the values of polynomial a~0~x^n^ + a~1~x^n−1^ + . . . + a~n−1~x + a~n~. In this problem you should evaluate its derivative. Remember that derivative is defined as a~0~nx^n−1^ + a~1~(n − 1)x^n−2^ + . . . + a~n−1~. All the input and output data will fit into integer, i.e. its absolute value will be less than 2^31^. ### 輸入限制 Your program should accept an even number of lines of text. Each pair of lines will represent one problem. The first line will contain one integer - a value for x. The second line will contain a list of integers a0, a1, ..., an−1, an, which represent a set of polynomial coefficients. Input is terminated by ¡EOF¿. ### 輸出限制 For each pair of lines, your program should evaluate the derivative of polynomial for the given value x and output it in a single line. ### 解題思路 1.這裡有一個比較特別的for寫法,就是沒有終止條件,是因為不知道會輸入多少個數字,會利用getchar()=='\n'的方式來判斷是否輸入完畢,前提是輸入要用scanf(for迴圈裡面的n之所以不寫裡面是因為要計算輸入的數量) 2.函式裡的exp是把X帶進去後次方所得的數,所以每過一個迴圈就必須*X 3.是從倒數第二項開始算,因為最後一項微分後會消掉,而微分後的係數是max-i,拿總長度去減當下i值,可以得出未微分時的次方 ### 程式碼 ```cpp= #include<bits/stdc++.h> using namespace std; int a[1000000]; int derivative(int x,int max){ long long sum=0; int exp=1; for(int i=max-1;i>=0;i--){ sum+=a[i]*exp*(max-i); exp*=x; } return sum; } int main(){ int x; while(cin>>x){ int n; for(n=0;;n++){ scanf("%d",&a[n]); if(getchar()=='\n'){ break; } } printf("%d\n",derivative(x,n)); } } ``` ## 測資 ### Sample input 7 1 -1 2 1 1 1 ### Sample output 1 5 ## 中文題目連結 [zerojudge f444](https://zerojudge.tw/ShowProblem?problemid=f444)