向前走莫回头❤

【NOIP 模拟题】[T2]one(递推)


【题解】【递推】

【约瑟夫问题。】

【考虑每次去掉一个人后都重新编号,把编号改为[0,n)来计算,那么,最后剩下的那个数的当前编号一定为0,倒推,那么上一轮当前点的编号为(n-i+1+x)%i,这样可以一直推会到第一轮。复杂度为O(N)】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,T;
int main()
{
	freopen("one.in","r",stdin);
	freopen("one.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	 {
	 	int x=0;
	 	scanf("%d",&n);
	 	for(int i=2;i<=n;++i) x=(n-i+1+x)%i;
	 	printf("%d\n",x+1);
	 }
	return 0;
}


posted @ 2016-10-05 18:36  lris0-0  阅读(85)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m