Trades FZU - 2281 (大数+贪心)
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 ;
}
Trades FZU - 2281 (大数+贪心)的更多相关文章
- Trades FZU - 2281 (贪心)(JAVA)
题目链接: J - Trades FZU - 2281 题目大意: 开始有m个金币, 在接下来n天里, ACMeow可以花费ci金币去买一个物品, 也可以以ci的价格卖掉这个物品, 如果它有足够的金 ...
- FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
C Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...
- Problem 2278 YYS (FZU + java大数)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2278 题目: 题意: 有n种卡牌,每种卡牌被抽到的概率为1/n,求收齐所有卡牌的天数的期望. 思路: 易推得公 ...
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...
- JAVA大数贪心
题意:01给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小. 思路分析: 一个简单的贪心,从高位到低位,判断当前位可否为 1 ,若可以,则将所有的数的这一位 ...
- FZU 2233 ~APTX4869 贪心+并查集
分析:http://blog.csdn.net/chenzhenyu123456/article/details/51308460 #include <cstdio> #include & ...
- FZU 2219【贪心】
思路: 因为工人造完一个房子就死了,所以如果m<n则还需要n-m个工人. 最优的方案应该是耗时长的房子应该尽快建,而且最优的是越多的房子在建越好,也就是如果当前人数不到n,只派一个人去分裂. 解 ...
- ZOJ - 3987 - Numbers (大数 + 贪心)
参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...
- FZU 2139——久违的月赛之二——————【贪心】
久违的月赛之二 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Stat ...
随机推荐
- 【hidden】微信小程序hidden属性使用示例
hidden属性用于隐藏标签,代码示例: <view hidden="{{!statusTag}}">我出来了~</view> <button bin ...
- leetcode-最长上升子序列LIS
转载原文地址:http://www.cnblogs.com/GodA/p/5180560.html 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7, ...
- opencv-学习笔记(5)形态学转变
opencv-学习笔记(4)形态学转变 本章讲了几种形态学操作 腐蚀erode 膨胀dilate 开运算MORPH_OPEN 闭运算MORPH_CLOSE 形态学梯度MORPH_GRADIENT 礼帽 ...
- 收割大厂offer需要具备的条件
转载出处 本人也一直在关注互联网,觉得还是有些了解.互联网要求是越来越高了,竞争的人太多了,不过你不用担心,个人觉得,你到了中层的水平,拿二线offer应该没问题,人多也有人多的好处,我比别人多努力一 ...
- zabbix 2.2.2 安装部署
zabbix 2.2.2版本与1.8.3版本安装过程略有不同,下面为实施步骤: 服务端:172.16.1.61 客户端:172.16.1.8 搭建zbbix软件 安装LAMP环境及依赖包 [root@ ...
- vuejs学习之 项目打包之后的首屏加载优化
vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...
- Python-期末练习
1.骑车与走路:我们的校园很大很大很大大大大大……,骑个自行车去办事会很快,比如取个快递了,到其他宿舍楼找个同(nv)学(you)了.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车.开锁.停车 ...
- iOS开发解决 jsonModel 属性跟系统的重复
-(id)initWithDic:(NSDictionary *)dic { if (self = [super init]) { [self setValuesForKeysWithDictiona ...
- iOS 出现错误reason: image not found的解决方案
在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...
- 3DMAX2016破解教程
首先,断网. 然后,下载3DMAX2016注册机. 然后,打开已经安装的3DMAX2016,会出现下图,点击激活按钮. 之后,以管理员身份打开3DMAX2016注册机,把申请号复制到注册机里面的请求码 ...