Trades
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
题目大意:给出每天物品的单价和本金,问买卖若干次后资金最多为多少?
解题思路:谷底买,山峰卖,用大数。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int T,cas=0;
Scanner in=new Scanner(System.in);
T=in.nextInt();
int[] a=new int[2005];
while((T--)>0) {
int n=in.nextInt(),m=in.nextInt();
for(int i=1;i<=n;i++) a[i]=in.nextInt();
BigInteger ans=BigInteger.valueOf(m);
for(int i=1;i<=n;) {
int j,k;
for(j=i;j+1<=n;j++) if(a[j+1]>a[j]) break;
if(j==n) break;
for(k=j+1;k+1<=n;k++) if(a[k+1]<a[k]) break;
BigInteger tmp1=ans.mod(BigInteger.valueOf(a[j]));
ans=ans.subtract(tmp1);
BigInteger tmp2=ans.divide(BigInteger.valueOf(a[j]));
tmp2=tmp2.multiply(BigInteger.valueOf(a[k]));
ans=tmp1.add(tmp2);
i=k+1;
}
ans=ans.mod(BigInteger.valueOf(1000000007));
cas++;
System.out.println("Case #"+cas+": "+ans);
}
in.close();
}
}