约瑟夫环

约瑟夫环

System Overload

Time Limit: 10 Seconds       Memory Limit: 32768 KB

Recently you must have experienced that when too many people use the BBS simultaneously, the net becomes very, very slow.
To put an end to this problem, the Sysop has developed a contingency scheme for times of peak load to cut off net access for some buildings of the university in a systematic, totally fair manner. Our university buildings were enumerated randomly from 1 to n. XWB is number 1, CaoGuangBiao (CGB) Building is number 2, and so on in a purely random order.
Then a number m would be picked at random, and BBS access would first be cut off in building 1 (clearly the fairest starting point) and then in every mth building after that, wrapping around to 1 after n, and ignoring buildings already cut off. For example, if n=17 and m=5, net access would be cut off to the buildings in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off CGB Building last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that building 2 is the last building selected.

Your job is to write a program that will read in a number of buildings n and then determine the smallest integer m that will ensure that our CGB Building can surf the net while the rest of the university is cut off.

Input Specification

The input file will contain one or more lines, each line containing one integer  n  with 3 <=  n  < 150, representing the number of buildings in the university.
Input is terminated by a value of zero (0) for  n .

Output Specification

For each line of the input, print one line containing the integer  m  fulfilling the requirement specified above.

Sample Input

3
4
5
6
7
8
9
10
11
12
0

Sample Output

2
5
2
4
3
11
2
3
8
16


题目大意:
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌
周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到
m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n].
递推公式:
f[1]=0;
f[i]=(f[i-1]+m)%i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值,最后结果是f[n]。因为实际生活中编号总是从1开始,我们输出f[n]+1。

这个题目是知道,开始报数的是1号大楼,但是由于它出队,所以可以把它看作他报得是m。而知道最终出队是2号大楼。让我们求最小的m。那么可以简单的把1号直接忽略,然后从2号开始报数。这样只需要f[n-1]=0,即可。将2~n大楼编号重新映射到0~n-2
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. int n;
  5. int solve(int m)
  6. {
  7. //由于开始XWBy一号大楼开始直接出队,所以从2~n进行编号0~n-2
  8. int f= 0;
  9. for( int i= 2;i<n;i++)
  10. f=(f+m)%i;
  11. if(f== 0) return 1;
  12. //编号为0,对应2号大楼
  13. return 0;
  14. }
  15. int main()
  16. {
  17. while( cin>>n&&n)
  18. {
  19. int m= 1; //每隔m个大楼,就是报m
  20. while(!solve(m))
  21. m++;
  22. cout<<m<< endl;
  23. }
  24. return 0;
  25. }

LA3882 And Then There Was One
题意:n个人,从m开始报数,报k的人出去。
最后一个出去的是谁?

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. const int maxn = 10000 + 2;
  5. int f[maxn];
  6. int main() {
  7. int n, k, m;
  8. while( scanf( "%d%d%d", &n, &k, &m) == 3 && n) {
  9. f[ 1] = 0;
  10. for( int i = 2; i <= n; i++)
  11. {
  12. f[i] = (f[i -1] + k) % i;
  13. // cout<<"f[ "<<i<<" ] "<<"--------"<<f[i]<<endl;
  14. }
  15. int ans = (m - k + 1 + f[n]) % n;
  16. if (ans <= 0) ans += n;
  17. printf( "%d\n", ans);
  18. }
  19. return 0;
  20. }
  21. /**
  22. 8 5 3
  23. 100 9999 98
  24. 10000 10000 10000
  25. 0 0 0
  26. 1
  27. 93
  28. 2019
  29. */

LA4727
题意:同样n个人,报k的出去。
问 最后三个出去的人 分别是谁?

  1. #include <iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. const int maxn = 600000;
  5. int f[maxn];
  6. int main()
  7. {
  8. int n,k;
  9. int T; scanf( "%d",&T);
  10. while(T--)
  11. {
  12. int a1,a2,a3;
  13. scanf( "%d%d",&n,&k);
  14. f[ 1]= 0; //只剩一个人的话,出去的人编号为0
  15. for( int i= 2;i<=n;i++) f[i]=(f[i -1]+k)%i; //推出n个人时,最后出去的人编号。
  16. a1=f[n]+ 1;
  17. f[ 2]=(k -1)% 2; //只剩两个人,那么倒数第二个人出去的编号就是 (k-1)%2
  18. for( int i= 3;i<=n;i++) f[i]=(f[i -1]+k)%i; // 推出n个人时,倒数第二个人出去的编号。
  19. a2=f[n]+ 1;
  20. f[ 3]=(k -1)% 3;
  21. for( int i= 4;i<=n;i++) f[i]=(f[i -1]+k)%i;
  22. a3=f[n]+ 1;
  23. cout<<a3<< ' '<<a2<< ' '<<a1<< endl;
  24. }
  25. return 0;
  26. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值