Project Euler problem 61
来源:互联网 发布:微博个性域名怎么删除 编辑:程序博客网 时间:2024/05/22 13:51
题意很明了。
然后我大概的做法就是暴搜了
先把每个几边形数中四位数的处理出来。
然后我就DFS回溯着找就行了。
比较简单吧。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <vector>using namespace std;vector<int>g[7], v[7][10000];int vis[7];void get_three(){ for(int i = 1; ; i++) { int x = i * (i + 1) / 2; if(x >= 10000) break; if(x >= 1000) g[0].push_back(x); }}void get_four(){ for(int i = 1; ; i++) { int x = i * i; if(x >= 10000) break; if(x >= 1000) g[1].push_back(x); }}void get_five(){ for(int i = 1; ; i++) { int x = i * (3 * i - 1) / 2; if(x >= 10000) break; if(x >= 1000) g[2].push_back(x); }}void get_six(){ for(int i = 1; ; i++) { int x = i * (2 * i - 1); if(x >= 10000) break; if(x >= 1000) g[3].push_back(x); }}void get_seven(){ for(int i = 1; ; i++) { int x = i * (i * 5 - 3) / 2; if(x >= 10000) break; if(x >= 1000) g[4].push_back(x); }}void get_eight(){ for(int i = 1; ; i++) { int x = i * (3 * i - 2); if(x >= 10000) break; if(x >= 1000) g[5].push_back(x); }}int a[7];int ans;int flag;void dfs(int deep){ if(flag) return; if(deep == 6) { if(a[5] % 100 == a[0] / 100) { flag = 1; printf("%d\n", a[0] + a[1] + a[2] + a[3] + a[4] + a[5]); return; } } for(int i = 0; i < 6; i++) { if(!vis[i]) { for(int j = 0; j < g[i].size(); j++) { int y = g[i][j]; if(!deep || y / 100 == a[deep - 1] % 100) { vis[i] = 1; a[deep] = y; dfs(deep + 1); vis[i] = 0; } } } }}int main(){ get_three(); get_four(); get_five(); get_six(); get_seven(); get_eight(); dfs(0); return 0;}
- Project Euler problem 61
- Project Euler:Problem 61 Cyclical figurate numbers
- Euler Project Problem 6
- project euler problem 11
- Project Euler Problem 81
- Project Euler Problem 60
- Project Euler Problem 59
- Project Euler Problem 58
- Project Euler Problem 57
- Project Euler Problem 16
- Project Euler Problem 30
- Project Euler Problem 31
- Project Euler Problem 32
- Project Euler Problem 14
- Project Euler Problem 15
- Project Euler Problem 17
- Project Euler - Problem 18
- Project Euler - Problem 67
- Goods类
- 大话遗传算法
- 《Essential C++》笔记二、面向过程的编程风格
- HDU 4767 Bell (中国剩余定理)
- 求最大公约数 C++
- Project Euler problem 61
- eclipse远程debug 设置
- linux 删除文件名为乱码的文件方法
- PHP架构-PHP内核
- 内存泄漏以及常见的解决方法
- LCP数组的实现和最长公共连续子串
- UVa 340 Master-Mind Hints (优化查找&复制数组)
- 《C专家编程》读后记
- struts2常用知识