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;
}

最新文章

  1. SQL Server2016 新功能实时查询统计信息
  2. SQL 2014 AlwaysOn 搭建
  3. norflash驱动编写笔记
  4. Git入门仅这篇就够了
  5. js获取鼠标位置的各种方法
  6. UITableViewCell的选中时的颜色设置
  7. EF错误记录
  8. Redefining already defined constructor
  9. C#和.NET版本
  10. html笔记01:顺序和无序列表
  11. 《C++程序设计》上半部读书笔记
  12. (Problem 2)Even Fibonacci numbers
  13. BSGS算法(大步小步算法)
  14. 雷林鹏分享:CodeIgniter 数据库操作
  15. 【数据结构】运输计划 NOIP2015提高组D2T3
  16. matlab global persistent变量
  17. linux 通过 mac地址 查询 ip 和 清除arp 缓存
  18. thinkphp5报错
  19. java 获取当前方法名
  20. 华清远见Linux设备驱动(每章小结)

热门文章

  1. 关于int,integer初始值问题
  2. springMVC配置Json
  3. (iOS逆向工程)class-dump 安装与使用
  4. GNU/Linux下LVM配置管理以及快照卷、物理卷、卷组、逻辑卷的创建和删除
  5. poj 1651 Multiplication Puzzle
  6. IIS使用Tip
  7. PLSQL Develop PlugIn 之脚本自动匹配补全工具CnPlugin
  8. PAT A 1030. Travel Plan (30)【最短路径】
  9. Linux第02天
  10. linux 使用命令笔记