hdu 1443 + cug1015 约瑟夫问题变型
来源:互联网 发布:mac系统解锁打不开了 编辑:程序博客网 时间:2024/05/05 02:56
直接暴力会超时死的不能再死,然后用数组每次查找下一个被干掉的人,如果小于k,干掉。cug的不能用该方法做,会超时,因为数据量小,直接用hdu的方法就把前面的结果算出来保存起来,最后直接AC。
#include <iostream>#include <string>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstdio> using namespace std;const int maxn = 20 + 2 ;long long f[maxn];long long ans[maxn]; int main(){ int k ; memset(f , 0 , sizeof(f)); while(scanf("%d", &k )!= EOF && k) { if(f[k] != 0) { cout << f[k] << endl; continue; } long long m = 1; int n = 2 * k; ////保证k轮不去掉好人 for(int i = 1 ; i <= k ;i ++ ) { ans[i] = (ans[i-1] + m - 1) % ( n - i + 1);//下一个要去掉的人 if(ans[i] < k) //如果去掉了好人 { i = 0;//说明当前m值不合适,重新开始开始试验 m++;//间隔加1 } } f[k] = m; cout << f[k] << endl; } return 0;}
//////////cug#include <iostream>#include <string>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 20 ;long long f[maxn] = {0 , 2 , 7 , 5 ,30 , 169 , 441 , 1872 , 7632 , 1740 , 93313 , 459901 , 1358657 , 2504881 , 13482720 , 25779600 , 68468401 , 610346880 , 1271932200 , 327280800 };int main(){ int k; while(scanf("%d", &k )!= EOF && k) { cout << f[k] << endl; } return 0;}
0 0
- hdu 1443 + cug1015 约瑟夫问题变型
- UVA 1452-Jump(约瑟夫环问题变型)
- HDU 1443 约瑟夫问题
- HDU 1443 Joseph 约瑟夫环问题
- hdu 约瑟夫问题
- HDU 5691 --tsp变型
- 汉诺塔问题及其变型
- POJ 1012 && HDU 1443 Joseph(约瑟夫问题)
- hdu 5691 Sitting in Line(百度之星A轮) 状压_旅行商问题变型
- 动态规划,背包问题变型
- HDU 5643:King's Game 约瑟夫问题
- HDU 2955 Robberies(01背包变型)
- hdu 1503 最长公共子序列变型
- 约瑟夫问题、约瑟夫环
- HDU-4841 圆桌问题 STL模拟约瑟夫问题
- hdu 1443 joseph (约瑟夫环模拟)
- hdu 1443 Joseph (约瑟夫环)
- HDU 1443 Joseph 约瑟夫环 暴力
- Hacking HP Printers for Fun & Profit
- Vector容器的基本用法
- Kafka 学习笔记(七)之Kafka Consistency
- Linux 将一个用户添加到某一用户组中
- 默认网关的疑问?????
- hdu 1443 + cug1015 约瑟夫问题变型
- leetcode[125]:Valid Palindrome
- UILabel自适应里面的文字,自动调整宽度和高度
- 每对顶点间的最短距离——floyd算法
- HadoopCDH4.2.1 Pseudo-Distributed 安装过程
- Mysql第六天 查询 1
- POJ 3468 A Simple Problem with Integers(BIT)
- 冒泡排序
- DLL中调用约定和名称修饰(一)