Problem 2281 Trades

Accept: 114    Submit: 612
Time Limit: 1000 mSec    Memory Limit : 262144 KB

 Problem Description

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

23 11 2 34 11 2 1 2

 Sample Output

Case #1: 3Case #2: 4

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

题意:就是做生意就是在低估买入,高峰卖出;

思路1:用java大数类解决

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();
            } 
    }


怎么用c++来解决这个问题呢?

贪心+大数压位

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long int ll;
const ll mod = 1e9;
ll sum[6000];
int len,j;
ll c;
ll a[2005];
int main(){
    int t, n, cas = 0;
    ll m;
    scanf("%d",&t);
    while(t--){
        memset(sum,0,sizeof(sum));
        sum[0] = 1;
        len = 1;
        scanf("%d%I64d",&n,&m);
        for(c=0,j=0; j < len; j++){
            ll p= sum[j]*m+c;
            sum[j]= p % mod;      //mod进制压位;
            c=p / mod;
        }
        sum[j] = c;
        if(c) len++;
        for(int i = 1; i <= n; i++)
            scanf("%I64d",&a[i]);
        for(int i = 2; i <= n; i++)
            if(a[i]>a[i-1]){
                ll p = 0;
                for(int j = len-1; j >= 0; j--){
                    p = p*mod+sum[j];
                    sum[j] = p/a[i-1];
                    p%=a[i-1];
                }
                ll v = p;
                ll c = 0;
                for(j = 0; j < len; j++){
                    p = sum[j]*a[i]+c;
                    sum[j] = p%mod;
                    c = p/mod;
                }
                sum[j] = c;
                if(c)
                    len++;
                sum[0] += v;
                for(j = 0; j < len; j++)
                    if(sum[j]>=mod){
                        sum[j+1] += sum[j]/mod;
                        sum[j]%=mod;
                    }
                    else
                        break;
                if(j==len)
                    len++;
            }
        ll ans = 0;
        for(int i = len-1; i >= 0; i--){
            ans = ans*mod+sum[i];
            ans %= mod+7;
        }
        printf("Case #%d: %I64d\n",++cas,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值