[csu/coj 1619] 递归
2024-08-27 20:51:51
题意:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1619
思路:由于式子具有递归的性质,考虑递归解,中间结果会超64位int,需用大数。另外自己写了个分数类,见代码。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e5 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } const int maxI = 1e8;
const int Len = ; struct BigInt { vi num;
bool symbol; BigInt() { num.clear(); symbol = ; }
BigInt(int x) { symbol = ; if (x < ) { symbol = ; x = -x; } num.push_back(x % maxI); if (x >= maxI) num.push_back(x / maxI); }
BigInt(bool s, vi x) { symbol = s; num = x; }
BigInt(char s[]) {
int len = strlen(s), x = , sum = , p = s[] == '-';
symbol = p;
for (int i = len - ; i >= p; i--) {
sum += (s[i] - '') * x;
x *= ;
if (x == 1e8 || i == p) {
num.push_back(sum);
sum = ;
x = ;
}
}
while (num.back() == && num.size() > ) num.pop_back();
} void push(int x) { num.push_back(x); } BigInt abs() const { return BigInt(false, num); } bool smaller(const vi &a, const vi &b) const {
if (a.size() != b.size()) return a.size() < b.size();
for (int i = a.size() - ; i >= ; i--) {
if (a[i] != b[i]) return a[i] < b[i];
}
return ;
} bool operator < (const BigInt &p) const {
if (symbol && !p.symbol) return true;
if (!symbol && p.symbol) return false;
if (symbol && p.symbol) return smaller(p.num, num);
return smaller(num, p.num);
} bool operator > (const BigInt &p) const {
return p < *this;
} bool operator == (const BigInt &p) const {
return !(p < *this) && !(*this < p);
} bool operator >= (const BigInt &p) const {
return !(*this < p);
} bool operator <= (const BigInt &p) const {
return !(p < *this);
} vi add(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < a.size(); i++) {
x += a[i];
if (i < b.size()) x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
for (int i = a.size(); i < b.size(); i++) {
x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
if (x) c.push_back(x);
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi sub(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < b.size(); i++) {
x += maxI + a[i] - b[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
for (int i = b.size(); i < a.size(); i++) {
x += maxI + a[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi mul(const vi &a, const vi &b) const {
vi c;
c.resize(a.size() + b.size());
for (int i = ; i < a.size(); i++) {
for (int j = ; j < b.size(); j++) {
LL tmp = (LL)a[i] * b[j] + c[i + j];
c[i + j + ] += tmp / maxI;
c[i + j] = tmp % maxI;
}
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi div(const vi &a, const vi &b) const {
vi c(a.size()), x(, ), y(, ), z(, ), t(, );
y.push_back();
for (int i = a.size() - ; i >= ; i--) {
z[] = a[i];
x = add(mul(x, y), z);
if (smaller(x, b)) continue;
int l = , r = maxI - ;
while (l < r) {
int m = (l + r + ) >> ;
t[] = m;
if (smaller(x, mul(b, t))) r = m - ;
else l = m;
}
c[i] = l;
t[] = l;
x = sub(x, mul(b, t));
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} BigInt operator + (const BigInt &p) const{
if (!symbol && !p.symbol) return BigInt(false, add(num, p.num));
if (!symbol && p.symbol) return *this >= p.abs()? BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num));
if (symbol && !p.symbol) return (*this).abs() > p? BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num));
return BigInt(true, add(num, p.num));
} BigInt operator - (const BigInt &p) const {
return *this + BigInt(!p.symbol, p.num);
} BigInt operator * (const BigInt &p) const {
BigInt res(symbol ^ p.symbol, mul(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == ) res.symbol = false;
return res;
} BigInt operator / (const BigInt &p) const {
if (p == BigInt()) return p;
BigInt res(symbol ^ p.symbol, div(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == ) res.symbol = false;
return res;
} BigInt operator % (const BigInt &p) const {
return *this - *this / p * p;
} BigInt operator += (const BigInt &that) {
return *this = *this + that;
}
BigInt operator -= (const BigInt &that) {
return *this = *this - that;
}
BigInt operator *= (const BigInt &that) {
return *this = *this * that;
}
BigInt operator /= (const BigInt &that) {
return *this = *this / that;
}
BigInt operator %= (const BigInt &that) {
return *this = *this % that;
} void show() const {
if (symbol) putchar('-');
printf("%d", num[num.size() - ]);
for (int i = num.size() - ; i >= ; i--) {
printf("%08d", num[i]);
}
//putchar('\n');
} int TotalDigit() const {
int x = num[num.size() - ] / , t = ;
while (x) {
x /= ;
t++;
}
return t + (num.size() - ) * Len;
} friend inline ostream & operator << (ostream & os, BigInt t1){
t1.show();
return os;
} friend inline istream & operator >> (istream & is, BigInt &t1){
char s[];
scanf("%s", s);
t1 = BigInt(s);
return is;
}
}; template<class T>
struct Fraction {
T a, b;
Fraction(T a, T b): a(a), b(b) {}
Fraction() {}
Fraction operator + (const Fraction &that) const {
T x = a * that.b + b * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator - (const Fraction &that) const {
T x = a * that.b - b * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator * (const Fraction &that) const {
T x = a * that.a, y = b * that.b;
return Fraction(x, y);
}
Fraction operator / (const Fraction &that) const {
T x = a * that.b, y = b * that.a;
return Fraction(x, y);
}
Fraction operator += (const Fraction &that) {
return *this = *this + that;
}
Fraction operator -= (const Fraction &that) {
return *this = *this - that;
}
Fraction operator *= (const Fraction &that) {
return *this = *this * that;
}
Fraction operator /= (const Fraction &that) {
return *this = *this / that;
}
Fraction operator ! () const {
return Fraction(b, a);
}
}; typedef BigInt bi;
typedef Fraction<BigInt> fb; fb get(int id, int n) {
int x;
cin >> x;
fb ans(x, );
if (id + < n) ans += !get(id + , n);
return ans;
} void print(fb num) {
cout << num.a / num.b;
if (num.a % num.b > ) {
cout << " ";
num.a %= num.b;
print(!num);
}
else cout << endl;
} void solve(fb num) {
if (num.a % num.b == ) {
cout << num.a / num.b << endl;
return ;
}
if (num.a * num.b < ) {
if (num.a > ) {
num.a *= -;
num.b *= -;
}
cout << num.a / num.b - << " ";
num.a += (num.a / num.b - ) * - * num.b;
}
else {
cout << num.a / num.b << " ";
num.a %= num.b;
}
if (num.a < ) {
num.a *= -;
num.b *= -;
}
print(!num);
} int main() {
//freopen("in.txt", "r", stdin);
int n, m, cas = ;
while (cin >> n >> m, n || m) {
cout << "Case " << ++ cas << ":" << endl;
fb num1 = get(, n);
fb num2 = get(, m);
solve(num1 + num2);
solve(num1 - num2);
solve(num1 * num2);
solve(num1 / num2);
}
return ;
}
最新文章
- jquery 操作
- Spring基础—— 泛型依赖注入
- python(14)类,方法,对象,实例
- An unexpected exception occurred while creating a change object. see the error log for more details
- android下activity中多个listview只允许主界面滚动
- mac中用命令行运行mysql
- CAS认证(3):验证用户信息
- 配置SHH集群
- Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform
- 在Windows上安装MySQL(免安装ZIP版)
- win8.1远程连接Redis数据库
- ENetwork Basic Configuration PT Practice SBA
- Linux上的文件搜索
- 基于Java实现红黑树的基本操作
- 关于db2处理特殊字段出现异常java.io.charConversionException
- 【XSY2703】置换 数学 置换 DP
- Django App(六) Customing Admin Form
- Graph
- 超链接中 utm_source, utm_medium 等参数的含义是什么?
- 详解 Webpack+Babel+React 开发环境的搭建