首页 > 代码库 > 【UVa】Jump(dp)

【UVa】Jump(dp)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4198

约瑟夫变形

根据f(n)=(f(n-1)+m)%n

因为是求倒数第几个

那么我们只要求出f(倒数第几个)的值然后再带进去即可。

(没理解的自行面壁。。)

而f(倒数第几个)=(m-1)%倒数第几个

然后就行了。。

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }int getans(int t, int k, int n) {	int f=(k-1)%t;	for1(i, t+1, n) f=(f+k)%i;	int ret=(f+1)%n;	return ret==0?n:ret;}int main() {	int n, k, cs=getint();	while(cs--) {		read(n); read(k);		printf("%d %d %d\n", getans(3, k, n), getans(2, k, n), getans(1, k, n));	}	return 0;}

  

【UVa】Jump(dp)