奇怪的贸易
【问题描述】
刚结束了CS战斗的小D又进入了EVE的游戏世界,在游戏中小D是一名商人,每天要做的事情就是在这里买东西,再运到那里去卖。这次小D来到了陌生的X星,X星上有n种货物,小D决定每种都买走一些,他用ai来表示第i种货物购买的数量,X星人对物品的单价有特别的决定方式。他们首先会选择一个基本价x,第一种物品单价为x,第二种物品单价为x^2,第三种物品单价为x^3……第i种物品单价为x^i。结算总价时,你还需要给他们一笔手续费a0,小D不知道自己带的钱是否能够进行这笔交易,所以请你帮助他计算这笔交易他要支付的总金额是多少。
【输入】
第一行两个数分别表示基准价x (x<=10),物品种数n (n<=100000);
第二行一个数,手续费a0 (a0<=100);
接下来的n行每行一个数,第i行表示第i种物品购买的数量(ai<=100)。
【输出】
输出结果的最后100位,若不足100位请高位用零补足。
【样例输入输出】
|
trade.in |
trade.out |
2 3 4 3 2 1 |
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026
|
【数据规模】
对20%的数据,n<=10;
对50%的数据,n<=200;
对100%的数据,n<=100000;
/******************************************************************************************************
** Copyright (C) 2011.07.01-2013.07.01
** Author: famousDT <13730828587@163.com>
** Edit date: 2011-10-25
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <time.h>
#include <set>
#include <list>
#include <stack>
#include <string>
#include <iostream>
#include <assert.h>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll
#include <algorithm>
using namespace std;
typedef long long ll;
#define MY_PI acos(-1)
#define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
#define MY_MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define MY_ABS(a) (((a) >= 0) ? (a) : (-(a)))
#define MY_INT_MAX 0x7fffffff
/*==========================================================*\
| y = a0 + a1*x^1 + a2 * x^2 + ... + an-1 * x^(n-1) +an * x^n
| y = a0 + x*(a1 + x*(... an-3 + x*(an-2 + x*(an-1 + anx))
| 涉及到高精度加和高精度乘,但应题目要求,只需要求出最后100位
\*==========================================================*/
int x, n;
int a[100005];
FILE *in, *out;
#define DIGIT 5
#define DEPTH 100000
#define MAX 20000 + 1
typedef int bignum_t[MAX + 1];
void write(const bignum_t a, ostream& os = cout) {
int i, j;
char s[10000];
int index = 0;
for (a[i = a[0]], i-- ; i; i--)
for (j = DEPTH / 10; j; j /= 10)
s[index++] = ((a[i] / j) % 10) + '0';
s[index] = '\0';
for (i = index - 100; i < index; ++i) printf("%c", s[i]);
printf("\n");
}
void add(bignum_t a, const int b) {
a[0] = 100;
int i = 1;
for (a[1] += b; a[i] >= DEPTH && i < a[0]; a[i + 1] += a[i] / DEPTH, a[i] %= DEPTH, i++);
for (; a[a[0]] >= DEPTH; a[a[0]+1] = a[a[0]] / DEPTH, a[a[0]] %= DEPTH, a[0]++);
}
void mul(bignum_t a, const int b){
a[0] = 100;
int i;
for (a[1] *= b, i = 2; i <= a[0]; i++) {
a[i] *= b;
if (a[i - 1] >= DEPTH)
a[i] += a[i - 1] / DEPTH, a[i - 1] %= DEPTH;
}
for (; a[a[0]] >= DEPTH; a[a[0] + 1] = a[a[0]] / DEPTH, a[a[0]] %= DEPTH, a[0]++);
for (; !a[a[0]]&&a[0] > 1; a[0]--);
}
int main()
{
in = freopen("trade.in", "rt", stdin);
out = freopen("trade.out", "wt", stdout);
bignum_t ans;
memset(ans, 0, sizeof(bignum_t));
scanf("%d%d", &x, &n);
int i;
for (i = 0; i <= n; ++i) scanf("%d", &a[i]);
add(ans, a[n]);
for (i = n - 1; i >= 0; --i) {
mul(ans, x);
add(ans, a[i]);
}
ans[0] = 100;
write(ans);;
system("pause");
return 0;
}
本文介绍了一种特殊的贸易结算方式,其中涉及到了高精度的数学运算。玩家小D需要在一个拥有n种货物的星球上购买商品,并根据独特的定价规则计算总费用。
(2011年NOIP冲刺模拟试题)&spm=1001.2101.3001.5002&articleId=6906949&d=1&t=3&u=f72bfe5eb0a34725a17c690d88d3bb67)
530

被折叠的 条评论
为什么被折叠?



