题意: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 ;
}

[csu/coj 1619] 递归的更多相关文章

  1. [csu/coj 1632]LCP

    题意:求一个串的出现次数超过1次的字串的个数 思路:对于一个后缀,出现在它后面的所有后缀与它的LCP的最大值就是应该增加的答案,当然这里没有考虑去重,但是却转化了问题,使得我们可以用最长公共前缀来统计 ...

  2. [csu/coj 1083]贪心

    题意:给定n个线段,问能不能把x,y,z个长度为1,2,3的线段不重合地放进去. 思路:首先如果n个线段长度比要放的长度之和小,则无解,否则先考虑放2和3,如果2和3放下了1肯定可以放下(这是显然的) ...

  3. [csu/coj 1078]多个序列的最长公共子序列

    题意:给n个序列,同一个序列里面元素互不相同,求它们的最长公共子序列. 思路:任取一个序列,对于这个序列里面的两个数ai,aj(i<j),如果对于其它每一个序列,都出现过ai,aj,且ai在aj ...

  4. [csu/coj 1079]树上路径查询 LCA

    题意:询问树上从u到v的路径是否经过k 思路:把树dfs转化为有根树后,对于u,v的路径而言,设p为u,v的最近公共祖先,u到v的路径必定是可以看成两条路径的组合,u->p,v->p,这样 ...

  5. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  6. csu 1806 & csu 1742 (simpson公式+最短路)

    1806: Toll Time Limit: 5 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 256  Solved: 74[Submit][Sta ...

  7. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]

    方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...

  8. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  9. Android 算法 关于递归和二分法的小算法

     // 1. 实现一个函数,在一个有序整型数组中二分查找出指定的值,找到则返回该值的位置,找不到返回 -1. package demo; public class Mytest { public st ...

随机推荐

  1. Jmeter 使用正则表达式提取响应结果中的值

    正则表达式提取的界面如下图: apply to: Main sample and sub-samples:作用于父节点取样器及对应子节点取样器Main sample only:仅作用于父节点取样器Su ...

  2. ApiPost V3创事记:一个痛并快乐着的创业故事

    前言 无论是对于国家,还是对于我们个人,2020年4月,是注定是一个不同往年的4月.一场突如起来的疫情打破了我们原来的生活曲线,让我们知道了什么是苦难,什么是团结,什么是坚持,什么是胜利. 一.大幕开 ...

  3. 使用 PyHamcrest 执行健壮的单元测试

    在 测试金字塔 的底部是单元测试.单元测试每次只测试一个代码单元,通常是一个函数或方法. 通常,设计单个单元测试是为了测试通过一个函数或特定分支的特定执行流程,这使得将失败的单元测试和导致失败的 bu ...

  4. Scapy的基本使用

    关于Scapy Scapy是一个可以让用户发送.侦听和解析并伪装网络报文的Python程序.这些功能可以用于制作侦测.扫描和攻击网络的工具. 换言之,Scapy 是一个强大的操纵报文的交互程序.它可以 ...

  5. ACNet: 特别的想法,腾讯提出结合注意力卷积的二叉神经树进行细粒度分类 | CVPR 2020

    论文提出了结合注意力卷积的二叉神经树进行弱监督的细粒度分类,在树结构的边上结合了注意力卷积操作,在每个节点使用路由函数来定义从根节点到叶子节点的计算路径,结合所有叶子节点的预测值进行最终的预测,论文的 ...

  6. linux下的.ssh文件夹路径等

    1.linux下的.ssh文件夹在~下,直接cd ~/.ssh即可 2.tp经过gd类处理过的水印图片格式为png 3.前端扒下别人家的网站如果自己本地打开有出现相同的代码段则有可能是js动态添加的, ...

  7. IBM WebSphere 远程代码执行漏洞安全预警通告

    近日,IBM发布安全通告称修复了一个WebSphere Application Server中一个潜在的远程代码执行漏洞(CVE-2018-1567).攻击者可以构造一个恶意的序列化对象,随后通过SO ...

  8. 2019-2020-1 20199329《Linux内核原理与分析》第十一周作业

    <Linux内核原理与分析>第十一周作业 一.本周内容概述: 学习linux安全防护方面的知识 完成实验楼上的<ShellShock 攻击实验> 二.本周学习内容: 1.学习& ...

  9. Jetson AGX Xavier刷机

    1. 准备一台电脑做主机(host),运行Ubuntu系统,我用的是虚拟机,运行的是Ubuntu 18.04系统. 2. 主机更换apt-get源,参见https://www.cnblogs.com/ ...

  10. numpy库的学习笔记

    一.ndarray 1.numpy 库处理的最基础数据类型是由同种元素构成的多维数组(ndarray),简称“数组”. 2.ndarray是一个多维数组的对象,ndarray数组一般要求所有元素类型相 ...