Skip to content

Instantly share code, notes, and snippets.

@aahung
Last active October 10, 2015 01:58
Show Gist options
  • Save aahung/b75c4b9175864e485388 to your computer and use it in GitHub Desktop.
Save aahung/b75c4b9175864e485388 to your computer and use it in GitHub Desktop.
ICPC Regionals 2010 :: Africa/Middle East - Arab Contest
//
// main.cpp
// testing1
//
// Created by Xinhong LIU on 17/5/14.
// Copyright (c) 2014 Xinhong LIU. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <list>
#include <string>
#include <stack>
using namespace std;
int main() {
long l, c, r;
while (cin >> l >> c >> r) {
if (l == 0 && c == 0 && r == 0) return 0;
if (l + r == 2 * c) {
cout << "AP " << (r + c - l) << endl;
} else if (l * r == c * c) {
cout << "GP " << (r * c / l) << endl;
}
}
return 0;
}
//
// main.cpp
// testing1
//
// Created by Xinhong LIU on 17/5/14.
// Copyright (c) 2014 Xinhong LIU. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <list>
#include <string>
#include <stack>
#include <vector>
using namespace std;
long nextC(long base, long sum) {
if (base < 10) {
return sum + base * base;
}
long ld = base % 10;
return nextC(base / 10, sum + ld * ld);
}
int main() {
long a, b;
while (cin >> a >> b) {
if (a + b == 0) {
return 0;
}
if (a == b) {
cout << a << " " << b << " 2" << endl;
continue;
}
//
vector<long> list = vector<long>();
list.push_back(a);
for (int i = 0; true; i++) {
long nextc = nextC(list[i], 0);
list.push_back(nextc);
bool loop = false;
for (vector<long>::iterator j = list.begin(); j + 1 != list.end(); j++) {
if (nextc == *j) {
loop = true;
break;
}
}
if (loop) {
break;
}
}
vector<long> b_list = vector<long>();
b_list.push_back(b);
for (int i = 0; true; i++) {
long nextc = nextC(b_list[i], 0);
b_list.push_back(nextc);
bool loop = false;
for (vector<long>::iterator j = b_list.begin(); j + 1 != b_list.end(); j++) {
if (nextc == *j) {
loop = true;
break;
}
}
if (loop) {
break;
}
}
bool found = false;
int min = list.size() + b_list.size() + 2;
for (int i = 0; i < list.size(); ++i) {
for (int j = 0; j < b_list.size(); ++j) {
if (list[i] == b_list[j] && min > i + j + 2) {
min = i + j + 2;
found = true;
}
}
}
if (!found) {
min = 0;
}
cout << a << " " << b << " " << min << endl;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 32003
int deepest_depth;
char line[N];
typedef struct r_t { // return type
int value_if_and;
int value_if_or;
} r_t;
void reset() {
deepest_depth = 0;
}
r_t eval(int begin, int end, int depth) {
r_t return_value;
return_value.value_if_or = 0;
return_value.value_if_and = 1;
int current_depth = 0;
int child_begin, child_end;
for (int i = begin; i < end; ++i) {
char c = line[i];
if (c == '(') {
if (current_depth == 0) {
current_depth++;
child_begin = i + 1;
} else current_depth++;
} else if (c == ')') {
current_depth--;
if (current_depth == 0) {
child_end = i;
r_t child_value
= eval(child_begin, child_end, depth + 1);
if (child_value.value_if_and == 1) {
return_value.value_if_or = 1;
}
if (child_value.value_if_or == 0) {
return_value.value_if_and = 0;
}
}
} else if (c == 'T' && current_depth == 0) {
return_value.value_if_or = 1;
} else if (c == 'F' && current_depth == 0) {
return_value.value_if_and = 0;
}
}
if (depth > deepest_depth)
deepest_depth = depth;
//#define DEBUG
#ifdef DEBUG
// print str first
for (int i = begin; i < end; ++i) {
printf("%c", line[i]);
}
printf("\tdepth: %d, value & %d | %d\n",
depth, return_value.value_if_and,
return_value.value_if_or);
#endif
return return_value;
}
char result_true[] = "true";
char result_false[] = "false";
int main() {
int case_i = 0;
while (gets(line) != NULL) {
if (line[1] == ')') return 0;
reset();
int begin = 1;
int end = strlen(line) - 1;
r_t return_value = eval(begin, end, 0);
char *result;
if (deepest_depth % 2 == 0) {
result = ((return_value.value_if_and)? result_true:result_false);
} else {
result = ((return_value.value_if_or)? result_true:result_false);
}
printf("%d. %s\n", ++case_i, result);
}
}
#include <stdio.h>
#define N 100003
#define INF 1e9
long long min_cost[N][3];
int n;
int main() {
int case_i = 0;
while (scanf("%d", &n) != EOF && n > 0 && ++case_i) {
long long cost[3];
scanf("%lld %lld %lld", &cost[0], &cost[1], &cost[2]);
// initial
min_cost[0][0] = INF;
min_cost[0][1] = cost[1];
min_cost[0][2] = cost[2] + cost[1];
for (int i = 1; i < n; ++i) {
scanf("%lld %lld %lld", &cost[0], &cost[1], &cost[2]);
min_cost[i][0] = min_cost[i - 1][0] + cost[0];
if (min_cost[i - 1][1] + cost[0] < min_cost[i][0])
min_cost[i][0] = min_cost[i - 1][1] + cost[0];
min_cost[i][1] = min_cost[i][0] + cost[1];
if (min_cost[i - 1][0] + cost[1] < min_cost[i][1])
min_cost[i][1] = min_cost[i - 1][0] + cost[1];
if (min_cost[i - 1][1] + cost[1] < min_cost[i][1])
min_cost[i][1] = min_cost[i - 1][1] + cost[1];
if (min_cost[i - 1][2] + cost[1] < min_cost[i][1])
min_cost[i][1] = min_cost[i - 1][2] + cost[1];
min_cost[i][2] = min_cost[i][1] + cost[2];
if (min_cost[i - 1][1] + cost[2] < min_cost[i][2])
min_cost[i][2] = min_cost[i - 1][1] + cost[2];
if (min_cost[i - 1][2] + cost[2] < min_cost[i][2])
min_cost[i][2] = min_cost[i - 1][2] + cost[2];
}
printf("%d. %lld\n", case_i, min_cost[n - 1][1]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
void work(long long *t_add, long long *t_all) {
long long ceil_t_all = 1;
while (ceil_t_all < *t_all) {
ceil_t_all *= 2;
}
*t_add = ceil_t_all - *t_all;
*t_all = ceil_t_all;
}
int main() {
long long g, t, a, d;
while (scanf("%lld %lld %lld %lld", &g, &t, &a, &d) != EOF
&& g != -1) {
long long r_per_group = t * (t - 1) / 2;
long long t_sec = a * g + d;
long long t_add;
work(&t_add, &t_sec);
long long r_all = r_per_group * g + t_sec - 1;
printf("%lld*%lld/%lld+%lld=%lld+%lld\n", g, a, t, d, r_all, t_add);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment