/
coupon_brute2.cc
40 lines (33 loc) · 897 Bytes
/
coupon_brute2.cc
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
#include <cstdio>
typedef unsigned long long ull;
const size_t MAXN = 1000;
long N, C[MAXN+1];
double A;
ull B;
FILE *fo;
ull TotalMoney(ull X, bool printIt) {
ull s=0;
for(long i=1; i<=N; ++i) {
if (X<10) X=0;
s+=X*C[i]; //the product doesn't fit in type 'long' - that's why we declare X as type 'ull'
if(printIt) fprintf(fo, "%llu\n", X);
X = (long)(A*X);
}
return s;
}
int main() {
long X;
FILE *fi = fopen("coupon.in", "r");
fscanf(fi, "%ld %lf %llu", &N, &A, &B);
for (long i = 1; i <= N; ++i) {
fscanf(fi, "%ld", &C[i]);
}
fclose(fi);
//the following loop does not have a main-body, it only searches for X - that's why it ends with ';'
for(X=B/C[1]+10; TotalMoney(X,false)>B; --X);
fo = fopen("coupon.out", "w");
fprintf(fo, "%llu\n", TotalMoney(X,false));
TotalMoney(X,true);
fclose(fo);
return 0;
}