This is a very easy problem.
ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n days, the unit price of GTX1920 in the ith day is Ci RMB. In other words, in the ith day, he can buy one GTX1920 with Ci RMB, or sell one GTX1920 to gain Ci RMB. He can buy or sell as many times as he wants in one day, but make sure that he has enough money for buying or enough GTX1920 for selling.
Now he wants to know, how many RMB can he get after the n days. Could you please help him?
It’s really easy, yeah?
Input
First line contains an integer T(1 ≤ T ≤20), represents there are T test cases.
For each test case: first line contains two integers n(1 ≤ n ≤2000) and m(0 ≤ m ≤1000000000). Following n integers in one line, the ith integer represents Ci(1 ≤ Ci ≤1000000000).
Output
For each test case, output "Case #X: Y" in a line (without quotes), where X is the case number starting from 1, and Y is the maximum number of RMB he can get mod 1000000007.
Sample Input
2
3 1
1 2 3
4 1
1 2 1 2
Sample Output
Case #1: 3
Case #2: 4
答案可以打到10^9000 上大数
题目大意:给出每天物品的单价和本金,问买卖若干次后资金最多为多少?
解题思路:谷底买,山峰卖,用大数。 被这个大数模板安排了 ans%1e9+7 模的过程会爆int
然后就一直WA 后面改了板子才过的
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const int MAXL = ;
const int MAXN = ;
const int DLEN = ;
class Big {
public:
int a[MAXL], len;
Big(const int b = ) {
int c, d = b;
len = ;
memset(a, , sizeof(a));
while(d > MAXN) {
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
Big(const char *s) {
int t, k, index, L;
memset(a, , sizeof(a));
L = strlen(s);
len = L / DLEN;
if(L % DLEN) len++;
index = ;
for(int i = L - ; i >= ; i -= DLEN) {
t = ;
k = i - DLEN + ;
if(k < ) k = ;
for(int j = k; j <= i; j++) t = t * + s[j] - '';
a[index++] = t;
}
}
Big operator/(const LL &b)const {
Big ret;
LL down = ;
for(int i = len - ; i >= ; i--) {
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - ] == && ret.len > ) ret.len--;
return ret;
}
bool operator>(const Big &T)const {
int ln;
if(len > T.len) return true;
else if(len == T.len) {
ln = len - ;
while(a[ln] == T.a[ln] && ln >= ) ln--;
if(ln >= && a[ln] > T.a[ln]) return true;
else return false;
} else return false;
}
Big operator+(const Big &T)const {
Big t(*this);
int big = T.len > len ? T.len : len;
for(int i = ; i < big; i++) {
t.a[i] += T.a[i];
if(t.a[i] > MAXN) {
t.a[i + ]++;
t.a[i] -= MAXN + ;
}
}
if(t.a[big] != ) t.len = big + ;
else t.len = big;
return t;
}
Big operator-(const Big &T)const {
int big;
bool flag;
Big t1, t2;
if(*this > T) {
t1 = *this;
t2 = T;
flag = ;
} else {
t1 = T;
t2 = *this;
flag = ;
}
big = t1.len;
for(int i = ; i < big; i++) {
if(t1.a[i] < t2.a[i]) {
int j = i + ;
while(t1.a[j] == ) j++;
t1.a[j--]--;
while(j > i) t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
} else t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - ] == && t1.len > ) {
t1.len--;
big--;
}
if(flag) t1.a[big - ] = - t1.a[big - ];
return t1;
}
LL operator%(const int &b)const {
LL d = ;
for(int i = len - ; i >= ; i--) d = ((d * (MAXN + )) % b + a[i]) % b;
return d;
}
Big operator*(const Big &T) const {
Big ret;
int i, j, up, temp, temp1;
for(i = ; i < len; i++) {
up = ;
for(j = ; j < T.len; j++) {
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN) {
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
} else {
up = ;
ret.a[i + j] = temp;
}
}
if(up != ) ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - ] == && ret.len > ) ret.len--;
return ret;
}
void print() {
printf("%d", a[len - ]);
for(int i = len - ; i >= ; i--) printf("%04d", a[i]);
}
};
int t, n, m, a[maxn], f[maxn];
int main() {
int cas = ;
sf(t);
while(t--) {
scanf("%d%d", &n, &m);
Big ans = m, temp;
for (int i = ; i <= n ; i++) sf(a[i]);
int i = , j, k;
while(i <= n) {
for (j = i ; j + <= n ; j++) if (a[j + ] > a[j]) break;
if (j == n) break;
for (k = j + ; k + <= n ; k++) if (a[k + ] < a[k]) break;
i = k + ;
temp = ans / a[j];
ans = ans + temp * (a[k] - a[j]);
}
LL ans1 = ans % mod;
printf("Case #%d: %lld\n", cas++, ans1);
}
return ;
}