您的位置:首页 > 其它

UVaOJ 11375 Matches

2013-09-01 20:39 337 查看
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2370

这题是递推,一开始没考虑会超精度WA了,后来打印了一下才知道得用高精度,于是手写高精度加法,减法。第一次重载运算符,第一次用10^8进制,结果都很顺利地实现了,实在欣慰。

递推方程比较简单。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<list>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#include<string>
#include<memory>
#include<map>
#include<sstream>
#pragma warning (disable : 4996)
#define mem(a) memset(a, 0, sizeof(a))
#define sl(a) strlen(a)
typedef long long LL;
typedef double dou;
const int Mod = 1000000007;
const int N = 2045;
using namespace std;

struct po
{
int a[100];
po operator + (const po& t) const
{
int i, d = 0, y;
po tem;
mem(tem.a);
for (i = 0; i < 100; ++i)
tem.a[i] = a[i] + t.a[i];
for (i = 0; i < 100; ++i)
{
y = tem.a[i] + d;
tem.a[i] = y % 100000000;
d = y / 100000000;
}
return tem;
}
po operator - (const po& t) const
{
int i, d = 0, c[100] = {0};
for (i = 0; i < 100; ++i) c[i] = a[i];
po tem;
mem(tem.a);
for (i = 0; i < 100; ++i)
{
if (c[i] < t.a[i])
{
c[i + 1]--, c[i] += 100000000;
tem.a[i] = c[i] - t.a[i];
}
else tem.a[i] = c[i] - t.a[i];
}
return tem;
}
}a
, s
;

int main()
{
/*freopen("outmy.txt", "w", stdout);*/
int i, j, n;
po tem;
a[0].a[0] = a[2].a[0] = a[3].a[0] = 1, a[4].a[0] = 2, a[5].a[0] = 5, a[6].a[0] = 7;
for (i = 7; i < N; ++i)
a[i] = a[i - 2] + a[i - 5] + a[i - 5] + a[i - 5] + a[i - 4] + a[i - 6] + a[i - 6] + a[i - 6] + a[i - 3] + a[i - 7];
for (i = 1; i < N; ++i) s[i] = s[i - 1] + a[i];
while (cin >> n)
{
mem(tem.a);
tem = n < 6 ? s
: s
- s[n - 6];
for (i = 99; i >= 0 && tem.a[i] == 0; --i);
printf("%d", tem.a[i]);
for (i--; i >= 0; --i) printf("%08d", tem.a[i]);
printf("\n");
}
/*for (j = 7; j < N; ++j)
{
mem(tem.a);
tem = s[j] - s[j - 6];
for (i = 99; i >= 0 && tem.a[i] == 0; --i);
printf("%d", tem.a[i]);
for (i--; i >= 0; --i) printf("%08d", tem.a[i]);
printf("\n");
}*/
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVaOJ 高精度