[csu/coj 1619] 递归

题意:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1619

思路:由于式子具有递归的性质,考虑递归解,中间结果会超64位int,需用大数。另外自己写了个分数类,见代码。

  1 #pragma comment(linker, "/STACK:10240000,10240000")

  2   

  3 #include <iostream>

  4 #include <cstdio>

  5 #include <algorithm>

  6 #include <cstdlib>

  7 #include <cstring>

  8 #include <map>

  9 #include <queue>

 10 #include <deque>

 11 #include <cmath>

 12 #include <vector>

 13 #include <ctime>

 14 #include <cctype>

 15 #include <set>

 16 #include <bitset>

 17 #include <functional>

 18 #include <numeric>

 19 #include <stdexcept>

 20 #include <utility>

 21   

 22 using namespace std;

 23   

 24 #define mem0(a) memset(a, 0, sizeof(a))

 25 #define mem_1(a) memset(a, -1, sizeof(a))

 26 #define lson l, m, rt << 1

 27 #define rson m + 1, r, rt << 1 | 1

 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)

 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)

 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)

 31 #define rep_down1(a, b) for (int a = b; a > 0; a--)

 32 #define all(a) (a).begin(), (a).end()

 33 #define lowbit(x) ((x) & (-(x)))

 34 #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) {}

 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}

 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}

 37 #define pchr(a) putchar(a)

 38 #define pstr(a) printf("%s", a)

 39 #define sstr(a) scanf("%s", a)

 40 #define sint(a) scanf("%d", &a)

 41 #define sint2(a, b) scanf("%d%d", &a, &b)

 42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)

 43 #define pint(a) printf("%d\n", a)

 44 #define test_print1(a) cout << "var1 = " << a << endl

 45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl

 46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl

 47 #define mp(a, b) make_pair(a, b)

 48 #define pb(a) push_back(a)

 49   

 50 typedef unsigned int uint;

 51 typedef long long LL;

 52 typedef pair<int, int> pii;

 53 typedef vector<int> vi;

 54   

 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};

 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };

 57 const int maxn = 1e5 + 7;

 58 const int md = 998244353;

 59 const int inf = 1e9 + 7;

 60 const LL inf_L = 1e18 + 7;

 61 const double pi = acos(-1.0);

 62 const double eps = 1e-6;

 63   

 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}

 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}

 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}

 67 template<class T>T condition(bool f, T a, T b){return f?a:b;}

 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}

 69 int make_id(int x, int y, int n) { return x * n + y; }

 70   

 71 const int maxI = 1e8;

 72 const int Len = 8;

 73   

 74 struct BigInt {

 75   

 76     vi num;

 77     bool symbol;

 78   

 79     BigInt() { num.clear(); symbol = 0; }

 80     BigInt(int x) { symbol = 0; if (x < 0) { symbol = 1; x = -x; } num.push_back(x % maxI); if (x >= maxI) num.push_back(x / maxI); }

 81     BigInt(bool s, vi x) { symbol = s;  num = x; }

 82     BigInt(char s[]) {

 83         int len = strlen(s), x = 1, sum = 0, p = s[0] == '-';

 84         symbol = p;

 85         for (int i = len - 1; i >= p; i--) {

 86             sum += (s[i] - '0') * x;

 87             x *= 10;

 88             if (x == 1e8 || i == p) {

 89                 num.push_back(sum);

 90                 sum = 0;

 91                 x = 1;

 92             }

 93         }

 94         while (num.back() == 0 && num.size() > 1) num.pop_back();

 95     }

 96   

 97     void push(int x) { num.push_back(x); }

 98   

 99     BigInt abs() const { return BigInt(false, num); }

100   

101     bool smaller(const vi &a, const vi &b) const {

102         if (a.size() != b.size()) return a.size() < b.size();

103         for (int i = a.size() - 1; i >= 0; i--) {

104             if (a[i] != b[i]) return a[i] < b[i];

105         }

106         return 0;

107     }

108   

109     bool operator < (const BigInt &p) const {

110         if (symbol && !p.symbol) return true;

111         if (!symbol && p.symbol) return false;

112         if (symbol && p.symbol) return smaller(p.num, num);

113         return smaller(num, p.num);

114     }

115   

116     bool operator > (const BigInt &p) const {

117         return p < *this;

118     }

119   

120     bool operator == (const BigInt &p) const {

121         return !(p < *this) && !(*this < p);

122     }

123   

124     bool operator >= (const BigInt &p) const {

125         return !(*this < p);

126     }

127   

128     bool operator <= (const BigInt &p) const {

129         return !(p < *this);

130     }

131   

132     vi add(const vi &a, const vi &b) const {

133         vi c;

134         c.clear();

135         int x = 0;

136         for (int i = 0; i < a.size(); i++) {

137             x += a[i];

138             if (i < b.size()) x += b[i];

139             c.push_back(x % maxI);

140             x /= maxI;

141         }

142         for (int i = a.size(); i < b.size(); i++) {

143             x += b[i];

144             c.push_back(x % maxI);

145             x /= maxI;

146         }

147         if (x) c.push_back(x);

148         while (c.back() == 0 && c.size() > 1) c.pop_back();

149         return c;

150     }

151   

152     vi sub(const vi &a, const vi &b) const {

153         vi c;

154         c.clear();

155         int x = 1;

156         for (int i = 0; i < b.size(); i++) {

157             x += maxI + a[i] - b[i] - 1;

158             c.push_back(x % maxI);

159             x /= maxI;

160         }

161         for (int i = b.size(); i < a.size(); i++) {

162             x += maxI + a[i] - 1;

163             c.push_back(x % maxI);

164             x /= maxI;

165         }

166         while (c.back() == 0 && c.size() > 1) c.pop_back();

167         return c;

168     }

169   

170     vi mul(const vi &a, const vi &b) const {

171         vi c;

172         c.resize(a.size() + b.size());

173         for (int i = 0; i < a.size(); i++) {

174             for (int j = 0; j < b.size(); j++) {

175                 LL tmp = (LL)a[i] * b[j] + c[i + j];

176                 c[i + j + 1] += tmp / maxI;

177                 c[i + j] = tmp % maxI;

178             }

179         }

180         while (c.back() == 0 && c.size() > 1) c.pop_back();

181         return c;

182     }

183   

184     vi div(const vi &a, const vi &b) const {

185         vi c(a.size()), x(1, 0), y(1, 0), z(1, 0), t(1, 0);

186         y.push_back(1);

187         for (int i = a.size() - 1; i >= 0; i--) {

188             z[0] = a[i];

189             x = add(mul(x, y), z);

190             if (smaller(x, b)) continue;

191             int l = 1, r = maxI - 1;

192             while (l < r) {

193                 int m = (l + r + 1) >> 1;

194                 t[0] = m;

195                 if (smaller(x, mul(b, t))) r = m - 1;

196                 else l = m;

197             }

198             c[i] = l;

199             t[0] = l;

200             x = sub(x, mul(b, t));

201         }

202         while (c.back() == 0 && c.size() > 1) c.pop_back();

203         return c;

204     }

205   

206     BigInt operator + (const BigInt &p) const{

207         if (!symbol && !p.symbol) return BigInt(false, add(num, p.num));

208         if (!symbol && p.symbol) return *this >= p.abs()? BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num));

209         if (symbol && !p.symbol) return (*this).abs() > p? BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num));

210         return BigInt(true, add(num, p.num));

211     }

212   

213     BigInt operator - (const BigInt &p) const {

214         return *this + BigInt(!p.symbol, p.num);

215     }

216   

217     BigInt operator * (const BigInt &p) const {

218         BigInt res(symbol ^ p.symbol, mul(num, p.num));

219         if (res.symbol && res.num.size() == 1 && res.num[0] == 0) res.symbol = false;

220         return res;

221     }

222   

223     BigInt operator / (const BigInt &p) const {

224         if (p == BigInt(0)) return p;

225         BigInt res(symbol ^ p.symbol, div(num, p.num));

226         if (res.symbol && res.num.size() == 1 && res.num[0] == 0) res.symbol = false;

227         return res;

228     }

229   

230     BigInt operator % (const BigInt &p) const {

231         return *this - *this / p * p;

232     }

233   

234     BigInt operator += (const BigInt &that)  {

235         return *this = *this + that;

236     }

237     BigInt operator -= (const BigInt &that)  {

238         return *this = *this - that;

239     }

240     BigInt operator *= (const BigInt &that)  {

241         return *this = *this * that;

242     }

243     BigInt operator /= (const BigInt &that)  {

244         return *this = *this / that;

245     }

246     BigInt operator %= (const BigInt &that)  {

247         return *this = *this % that;

248     }

249   

250     void show() const {

251         if (symbol) putchar('-');

252         printf("%d", num[num.size() - 1]);

253         for (int i = num.size() - 2; i >= 0; i--) {

254             printf("%08d", num[i]);

255         }

256         //putchar('\n');

257     }

258   

259     int TotalDigit() const {

260         int x = num[num.size() - 1] / 10, t = 1;

261         while (x) {

262             x /= 10;

263             t++;

264         }

265         return t + (num.size() - 1) * Len;

266     }

267   

268     friend inline ostream & operator << (ostream & os, BigInt t1){

269         t1.show();

270         return os;

271     }

272   

273     friend inline istream & operator >> (istream & is, BigInt &t1){

274         char s[22];

275         scanf("%s", s);

276         t1 = BigInt(s);

277         return is;

278     }

279 };

280   

281 template<class T>

282 struct  Fraction {

283     T a, b;

284     Fraction(T a, T b): a(a), b(b) {}

285     Fraction() {}

286     Fraction operator + (const Fraction &that) const {

287         T x = a * that.b + b * that.a, y = b * that.b;

288         return Fraction(x, y);

289     }

290     Fraction operator - (const Fraction &that) const {

291         T x = a * that.b - b * that.a, y = b * that.b;

292         return Fraction(x, y);

293     }

294     Fraction operator * (const Fraction &that) const {

295         T x = a * that.a, y = b * that.b;

296         return Fraction(x, y);

297     }

298     Fraction operator / (const Fraction &that) const {

299         T x = a * that.b, y = b * that.a;

300         return Fraction(x, y);

301     }

302     Fraction operator += (const Fraction &that)  {

303         return *this = *this + that;

304     }

305     Fraction operator -= (const Fraction &that)  {

306         return *this = *this - that;

307     }

308     Fraction operator *= (const Fraction &that)  {

309         return *this = *this * that;

310     }

311     Fraction operator /= (const Fraction &that)  {

312         return *this = *this / that;

313     }

314     Fraction operator ! () const {

315         return Fraction(b, a);

316     }

317 };

318   

319 typedef BigInt bi;

320 typedef Fraction<BigInt> fb;

321   

322 fb get(int id, int n) {

323     int x;

324     cin >> x;

325     fb ans(x, 1);

326     if (id + 1 < n) ans += !get(id + 1, n);

327     return ans;

328 }

329   

330 void print(fb num) {

331     cout << num.a / num.b;

332     if (num.a % num.b > 0) {

333         cout << " ";

334         num.a %= num.b;

335         print(!num);

336     }

337     else cout << endl;

338 }

339   

340 void solve(fb num) {

341     if (num.a % num.b == 0) {

342         cout << num.a / num.b << endl;

343         return ;

344     }

345     if (num.a * num.b < 0) {

346         if (num.a > 0) {

347             num.a *= -1;

348             num.b *= -1;

349         }

350         cout << num.a / num.b - 1 << " ";

351         num.a += (num.a / num.b - 1) * -1 * num.b;

352     }

353     else {

354         cout << num.a / num.b << " ";

355         num.a %= num.b;

356     }

357     if (num.a < 0) {

358         num.a *= -1;

359         num.b *= -1;

360     }

361     print(!num);

362 }

363   

364 int main() {

365     //freopen("in.txt", "r", stdin);

366     int n, m, cas = 0;

367     while (cin >> n >> m, n || m) {

368         cout << "Case " << ++ cas << ":" << endl;

369         fb num1 = get(0, n);

370         fb num2 = get(0, m);

371         solve(num1 + num2);

372         solve(num1 - num2);

373         solve(num1 * num2);

374         solve(num1 / num2);

375     }

376     return 0;

377 }
View Code

 

你可能感兴趣的:(递归)