标签:dp
题意是: 个数组成一个环,从第k个元素开始删除,每隔k个元素删一个,问最后删除的三个是什么
思路:约瑟夫问题变形,倒数第二个第三个求法与最后一个元素求法相同
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #define eps 1e-6 #define LL long long using namespace std; const int maxn = 500000 + 100; const int INF = 0x3f3f3f3f; //freopen("input.txt", "r", stdin); int f1[maxn], f2[maxn], f3[maxn], n, k; void cal_f() { f3[3] = (k-1) % 3; f2[3] = (k%2 == 0 ? (2+f3[3])%3 : (1+f3[3])%3); f1[3] = 3 - f2[3] - f3[3]; for(int i = 4; i <= n; i++) { f1[i] = (f1[i-1]+k) % i; f2[i] = (f2[i-1]+k) % i; f3[i] = (f3[i-1]+k) % i; } f1[n]++; f2[n]++; f3[n]++; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &k); cal_f(); //for(int i = 3; i <= n; i++) printf("%d %d %d\n", f3[i], f2[i], f1[i]); printf("%d %d %d\n", f3[n], f2[n], f1[n]); } return 0; }
uvalive 4727 jump跳跃(dp/约瑟夫问题变形)
标签:dp
原文地址:http://blog.csdn.net/u014664226/article/details/46494117