[hdu5218]DP-约瑟夫环变形

题意:n个人围成一圈,另外一个人最开始站在第一个人前面,每次从集合s里面随机选一个数x,这个人顺时针经过x个人后停下来,当前位置的前一个人出队,然后继续进行,求最后剩下的那个人的可能编号。

思路:由于只求最后一个人的编号,可以将一次操作后的人进行重编号,来进行状态转移,转化为子问题用dp来解决。dp方程比较容易写出,注意下细节就好了。

  1 #pragma comment(linker, "/STACK:102400000,102400000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d\n", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48
 49 typedef long long LL;
 50 typedef pair<int, int> pii;
 51 typedef vector<int> vi;
 52
 53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 55 const int maxn = 2e2 + 7;
 56 const int md = 10007;
 57 const int inf = 1e9 + 7;
 58 const LL inf_L = 1e18 + 7;
 59 const double pi = acos(-1.0);
 60 const double eps = 1e-6;
 61
 62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 67 int make_id(int x, int y, int n) { return x * n + y; }
 68
 69 int p[maxn], f[maxn][maxn], ans[maxn];
 70 int main() {
 71     //freopen("in.txt", "r", stdin);
 72     int T, n, m;
 73     cin >> T;
 74     while (T--) {
 75         cin >> n >> m;
 76         rep_up0(i, m) {
 77             sint(p[i]);
 78         }
 79         mem0(f);
 80         f[n][0] = true;
 81         for (int i = n - 1; i; i --) {
 82             for (int j = 0; j < n - i + 1; j ++) {
 83                 rep_up0(k, m) {
 84                     if ((p[k] + n - i) % (n - i + 1) == j) continue;
 85                     int sta, tmp = p[k] % (n - i + 1);
 86                     if (j >= tmp) sta = j - tmp;
 87                     else sta = n - i + 1 - tmp + j;
 88                     f[i][j] = f[i][j] || f[i + 1][sta];
 89                 }
 90             }
 91         }
 92         int cnt = 0;
 93         rep_up0(i, n) {
 94             if (f[1][i]) ans[cnt ++] = i + 1;
 95         }
 96         cout << cnt << endl;
 97         rep_up0(i, cnt) {
 98             printf("%d%c", ans[i], i == cnt - 1? ‘\n‘ : ‘ ‘);
 99         }
100     }
101     return 0;
102 }

时间: 2024-12-18 02:59:32

[hdu5218]DP-约瑟夫环变形的相关文章

Poj 3517 And Then There Was One(约瑟夫环变形)

简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周围的人全部出列. 想要求出最后剩下的那个人的在初始的时候的编号的话. f[1]=0; f[i]=(f[i-1]+m)%i;  (i>1) 可以推出剩下i个人内叫到m的时候的编号.注意这是逆推.推到最后初始的时候的情况 #include<stdio.h>

HDU 5643 King&#39;s Game | 约瑟夫环变形

经典约瑟夫环 1 int f[N] ={ 0 }; 2 for(int i=2; i<=n; i++) 3 { 4 f[i] = (f[i-1] + k) % i; 5 } 变形:k是变化的 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <queue> #in

uvalive 4727 jump跳跃(dp/约瑟夫问题变形)

 题意是: 个数组成一个环,从第k个元素开始删除,每隔k个元素删一个,问最后删除的三个是什么 思路:约瑟夫问题变形,倒数第二个第三个求法与最后一个元素求法相同 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #inclu

【约瑟夫环变形】UVa 1394 - And Then There Was One

首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更容易(...)共享一下~http://blog.csdn.net/chenguolinblog/article/details/8873444 问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数.求胜利者的编号. 编号0~(n-1)是有意义的,因为要

poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

题目链接: POJ  1012: http://poj.org/problem?id=1012 HDU 1443: http://acm.hdu.edu.cn/showproblem.php?pid=1443 约瑟夫环(百度百科): http://baike.baidu.com/view/717633.htm?fr=aladdin Description The Joseph's problem is notoriously known. For those who are not famili

(hdu step 2.2.2)Joseph(约瑟夫环变形问题)

题目: Joseph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2078 Accepted Submission(s): 1204   Problem Description The Joseph\\\\\\\'s problem is notoriously known. For those who are not familiar

约瑟夫环的变形

问题描述: 输入一个由随机数组成的数列(数列中每个数均是大于0的整数,长度已知),和初始计数值m.从数列首位置开始计数,计数到m后,将数列该位置数值替换计数值m,并将数列该位置数值出列,然后从下一位置从新开始计数,直到数列所有数值出列为止.如果计数到达数列尾段,则返回数列首位置继续计数.请编程实现上述计数过程,同时输出数值出列的顺序. 比如:输入的随机数列为:3,1,2,4,初始计数值m=7,从数列首位置开始计数(数值3所在位置) 第一轮计数出列数字为2,计数值更新m=2,出列后数列为3,1,4

UVALive 3882--And Then There Was One+约瑟夫环问题变形

题目链接:点击进入 题目意思大概和约瑟夫环问题差不多,唯一的不同点在于起点改成了m:刚开始的时候我想直接链表模拟算了,但是后面一看,数据太大,就改用公式做了.约瑟夫环的公式是:f(n)=(f(n-1)+k)%n ,对于这个题起点为m,所以答案就会变成ans=(f(n)+m-k+1)%n; ans有可能小于0,此时我们要给他加上一个n,ans+=n. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #

And Then There Was One(约瑟夫问题变形)

题目链接:http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5014   Accepted: 2685 Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbered 1, …

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o