-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerSequencesFromAdditionOfTerms.java
57 lines (49 loc) · 1.27 KB
/
IntegerSequencesFromAdditionOfTerms.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.iterative;
import java.util.*;
import java.io.*;
//Uva 927
public class IntegerSequencesFromAdditionOfTerms {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static int d, k, a[];
public static void main(String[] args) throws Exception{
int t = Integer.parseInt(reader.readLine());
String input[];
while(t-- != 0) {
input = reader.readLine().split(" ");
a = new int[Integer.parseInt(input[0]) + 1];
for(int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(input[i+1]);
}
d = Integer.parseInt(reader.readLine());
k = Integer.parseInt(reader.readLine());
process();
}
writer.close();
reader.close();
}
private static void process() throws Exception{
long sum = 0; int n = 0;
for(int i = 1; i <= 1000000; i++) {
sum += d*i;
if(k <= sum) {
n = i;
break;
}
}
writer.write(String.format("%d\n", solvePoly(n)));
}
private static long solvePoly(int n) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i] * pow(n, i);
}
return sum;
}
private static long pow(long b, long exp) {
long result = 1;
for(int i = 0; i < exp; i++)
result *= b;
return result;
}
}