bzoj2523 聪明的学生

bzoj第一题,ctsc2001.
黑书上的递归例题,我们定义time()函数,递归求解即可。
这个题用到了一个小技巧:可以使用枚举来搞算法。

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int n, m;
int p(int a, int b) {
	if(b > a) return b-a;
	else return b+3-a;
}
int times(int i, int j, int t1, int t2, int t3) {
	if(i == j) return t3;
	else if (i > j) return times(j, i-j, t2, t3, t1) + p(t1, t3);
	else return times(i, j-i, t1, t3, t2) + p(t2, t3);
}
struct ans
{
	int a, b, c;
	bool operator < (const ans &b) const {
		if(this->a != b.a) return this->a < b.a;
		else return this->b < b.b;
	}
};
int main() {
	//freopen("student.in", "r" , stdin);
	//freopen("out.out", "w", stdout);
	while(scanf("%d %d", &n, &m) && n != -1) {
		int p = 0;
		set<ans> aa;
		for(int i = 1; i <= m-1; i++) {
			int a = i;
			int b = m-i;
			int c = m;
			int k = n % 3;
			if(k == 0) k = 3;
			int xx = (k-1)%3;
			int yy = (k+1)%3;
			if(xx == 0) xx = 3;
			if(yy == 0) yy = 3;
			if(times(a, b, yy, xx, k) == n) {
				if(k == 1) {
					aa.insert((ans) {a+b, a, b} );
				}
				else if(k == 2) {
					aa.insert((ans) {b, a+b, a} );
				}
				else if(k == 3) {
					aa.insert((ans) {a, b, a+b} );
				}
				p++;
			}
			if(times(a, b, xx, yy, k) == n) {
				if(k == 1) {
					aa.insert((ans) {a+b, b, a} );
				}
				else if(k == 2) {
					aa.insert((ans) {a, a+b, b} );
				}
				else if(k == 3) {
					aa.insert((ans) {b, a, a+b} );
				}
				p++;
			}
		}
		cout << p/2 << endl;
		set<ans>::iterator it;
		for(it = aa.begin(); it != aa.end(); it++) {
			cout << (*it).a << ' ' << (*it).b << ' ' << (*it).c << endl;
		}
	}
	return 0;
}

posted on 2016-11-30 17:49  蒟蒻konjac  阅读(379)  评论(0编辑  收藏  举报