青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Brian Warehouse

Some birds aren`t meant to be caged, their feathers are just too bright... ...
posts - 40, comments - 16, trackbacks - 0, articles - 1

POJ 1012 2244 Joseph 問題詳解

Posted on 2010-08-17 13:42 Brian 閱讀(2132) 評論(3)  編輯 收藏 引用 所屬分類: POJ

約瑟夫環實在是太奇妙啦(我很高興我的這篇原創文章被不少人轉載了,雖然他們都沒有引用出處... ...)!
1012 Joseph
Description

The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.
2244 Eeny Meeny Moo

Description

Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow.
To put an end to this problem, the University of Ulm has developed a contingency scheme for times of peak load to cut off net access for some cities of the country in a systematic, totally fair manner. Germany's cities were enumerated randomly from 1 to n. Freiburg was number 1, Ulm was number 2, Karlsruhe was number 3, and so on in a purely random order.
Then a number m would be picked at random, and Internet access would first be cut off in city 1 (clearly the fairest starting point) and then in every mth city after that, wrapping around to 1 after n, and ignoring cities already cut off. For example, if n=17 and m=5, net access would be cut off to the cities 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 Ulm 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 city 2 is the last city selected.

Your job is to write a program that will read in a number of cities n and then determine the smallest integer m that will ensure that Ulm can surf the net while the rest of the country is cut off.

Input

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

Output

For each line of the input, print one line containing the integer m fulfilling the requirement specified above.
 
1012打表做法 C :
#include<stdio.h>
int a[14]={2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720};
int main()
{
 int i;
 while ( scanf("%d",&i), i != 0 )
  printf("%d\n",a[i-1]);
 return 0;
} // 這是從網上找的做法,號稱打表法,這些數據依舊要通過建立循環鏈表或是別的模擬法來求出。但是單純為了AC,這種做法真的是相當有效,講白了就是有目的的窮舉結果。
1012模擬法 C: 可惜呀可惜!這個總是 超時!我不知道是什么原因。但是思路是正確的,可能有些地方我沒有考慮到,看到這篇日志的人請指點。
#include<stdio.h>
int main()
{
 int i,m,k,cur,rest;
 
 while(1)
 {
  i=0; // the use ... sort of m in the question
  m=0;
  scanf("%d",&k);
  if (k == 0) break;
  while (1)
  {
   i++;
   rest=2*k; // good + bad guys
   cur=0;
   while (1)
   {
    cur=(cur+i-1)%rest; // find next from ZERO!
    if (cur >= k)
     rest--;
    else break;
   }
   if (rest == k)
   {
    m=i;
    break;
   }
  }
  printf("%d\n",m);
 }
 return 0;
}
對于 2244,建議看一個牛人的ACM博客: m.shnenglu.com/AClayton/archive/2007/11/06/35964.html
我就是看這篇博文的,很牛的一個人 AClayton ,寫的日期剛好是我生日,下面是其全部博文:
--------------------------------------------------------------------------------------------------------------------------------------------

 在沒有明白約瑟夫問題之前,只能用模擬來做.
      約瑟夫問題是這樣的:
      假設n個人,編號為1到n,排成一個圈,順時針從1開始數字m,數到m的人殺了,剩下的人繼續游戲.活到最后的一個人是勝利者.一般來說需要編程求解最后一個人的編號.
      思路是這樣的:
     假設當前剩下i個人(i<=n),顯然這一輪m要掛(因為總是從1開始數).經過這一輪,剩下的人是:1 2 3 ... m- 1 m + 1 ... i, 我們將從m+1開始的數映射成1, 則m+2對應2, n對應i - m, 1對應成i - m + 1  m - 1對應i - 1,那么現在的問題變成了已知i - 1個人進行循環報數m,求出去的人的序號。假設已經求出了i- 1個人循環報數下最后一個出去的人的序號X0,那么它在n個人中的序號X1=(X0+ m - 1) % n + 1,  最初的X0=1 ,反復迭代X0和X1可以求出.
     簡單約瑟夫問題的解法:
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse#include <stdio.h >
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehousemain()
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian WarehousePOJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse{
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
int n, m,i, s=0
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    printf( 
"N  M  =  "); scanf("%d%d ",&n,&
m);
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
for(i=2;i<=n;i++)s=(s+m)%
i;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    printf(
"The winner is %d\n ", s+1
);
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse}
  

      這倒題其實不是完全的約瑟夫問題,而是稍微變了形.呵呵,聰明的讀者自己發現!這一點費了我很久的時間,還害我逃了課被點名...
    這道題的我解法是這樣的.

POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse#include   <stdio.h >
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
int y(int n,int m)
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian WarehousePOJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
{
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
int s=1
,i;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
for(i=2;i<=n-1;i++
)
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse        s
=(s+m-1)%i+1
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
return s+1
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse}

POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehousemain()
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian WarehousePOJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse{
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
int
 m,n;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
while(1
)
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian WarehousePOJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
{
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    scanf(
"%d",&
n);
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
if(n==0)break
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse     
for(m=1
;;)
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian WarehousePOJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse     
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
{
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse         
if(y(n,m)==2)break
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse         m
++
;
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse     }

POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse   
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    printf(
"%d\n",m);
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    
POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse    }

POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse}

POJ 1012 2244 Joseph 問題詳解 - Icho - Brian Warehouse

       讀一個數處理一個數, Memory 68K,時間31MS,如果覺得效率不高. 優化的辦法是打表~

-----------------------------------------------------------------------------------------------------------------------------
由此可見,將問題化歸為數學問題,用初等高等或是數論來解決的能力是多么重要。
下面是我根據AClayton的思路簡化后的代碼,可直接AC: 注意,題目讓你先讓City 1 掛掉
2244  編譯器 C :
#include<stdio.h>
void main()
{
 
int i,r,m,n;
 
while (scanf("%d",&n) && n)
 {
    
for (m=1; ; m++)
    {
     
for (r=1,i=2; i<=n-1; i++)
      r
=(r+m-1)%+ 1;
     
if(r==1break;
    }
    printf(
"%d\n",m);
 }
// 164K  16MS
 

Feedback

# re: POJ 1012 2244 Joseph 問題詳解  回復  更多評論   

2013-04-27 12:42 by libai
你這也叫詳解???、我擦

# re: POJ 1012 2244 Joseph 問題詳解[未登錄]  回復  更多評論   

2013-09-16 13:31 by Icho
@libai

好吧。。人艱不拆。。。中間那段黑體我感覺說到點子上了

# re: POJ 1012 2244 Joseph 問題詳解  回復  更多評論   

2014-03-13 21:54 by shang
第一題的超時是因為 輸入數據有很多組 你要是每次都算的話就會超 所以要先打表存起來 O(1)詢問就不會超了
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品美女久久久| 欧美在线地址| 国产亚洲免费的视频看| 久久九九精品| 欧美日韩亚洲一区二区三区在线| 久久成人av少妇免费| 一区二区在线免费观看| 久久精品国产精品亚洲综合| 午夜精品久久久久久久| 国产综合视频在线观看| 欧美成人精品1314www| 最新69国产成人精品视频免费| 久久久午夜视频| 亚洲精品美女久久7777777| 欧美精品成人91久久久久久久| 欧美va亚洲va香蕉在线| 一本久久a久久精品亚洲| 国产精品欧美久久| 午夜国产精品影院在线观看| 久久精品99国产精品酒店日本| 亚洲高清不卡在线| 欧美国产亚洲另类动漫| 欧美一区二区视频在线观看| 亚洲精品网址在线观看| 欧美在线关看| 欧美国产日韩一区二区| 久久久久五月天| 91久久综合| 性欧美video另类hd性玩具| 亚洲第一级黄色片| 久久精品免费| 日韩一级黄色片| 欧美成人精品一区| 亚洲综合导航| 欧美一区三区二区在线观看| 免费亚洲婷婷| 欧美大片91| 国内精品福利| 久久成人18免费观看| 亚洲婷婷在线| 久色婷婷小香蕉久久| 老司机精品久久| 国产亚洲视频在线| 久久久在线视频| 久久国产精品一区二区| 国产一区二区三区四区在线观看| 亚洲一区二区在线免费观看| 国产精品手机视频| 午夜视频在线观看一区二区三区| 欧美国产精品人人做人人爱| 欧美在线视频不卡| 亚洲图片你懂的| 欧美人在线观看| 欧美77777| 日韩视频在线一区| 欧美在线电影| 久久成人免费电影| 亚洲激情av| 欧美a级片网| 欧美日本韩国一区二区三区| 亚洲永久网站| 久久综合久久综合这里只有精品| 一区二区三区免费看| 久久国产综合精品| 牛牛国产精品| 久久久久久久久久久久久久一区| 久久婷婷av| 亚洲国产清纯| 性欧美大战久久久久久久久| 日韩网站在线| 久久这里有精品15一区二区三区| 亚洲视频中文字幕| 9久草视频在线视频精品| 欧美日本一区二区三区| 美女脱光内衣内裤视频久久影院| 亚洲欧美国产视频| 亚洲欧美日韩国产另类专区| 亚洲精品在线二区| 在线成人激情黄色| 麻豆精品一区二区综合av | 亚洲字幕一区二区| 亚洲黄色视屏| 欧美韩日一区二区| 亚洲国产老妈| 欧美日韩另类视频| 亚洲人成亚洲人成在线观看图片| 免费日韩av片| 亚洲激情小视频| 夜夜嗨av一区二区三区网页| 欧美日本在线播放| 亚洲午夜激情| 久久久久欧美精品| 亚洲高清资源| 国产精品久久久久三级| 午夜视频精品| 欧美不卡一区| 亚洲在线国产日韩欧美| 小黄鸭精品密入口导航| 欧美亚洲一区二区在线观看| 久久一区中文字幕| 国产精品国产三级国产普通话蜜臀 | 日韩视频一区二区三区在线播放| 欧美经典一区二区三区| 中文av字幕一区| 一区二区三区国产精品| 国产精品亚洲一区| 欧美精品97| 欧美不卡福利| 欧美va天堂在线| 久久se精品一区二区| 亚洲午夜在线视频| 91久久精品久久国产性色也91| 欧美一区午夜精品| 亚洲欧美日韩在线不卡| 宅男精品导航| 亚洲一区二区三区在线观看视频| 免费日韩av| 99精品热6080yy久久| 欧美在线一二三区| 9久re热视频在线精品| 亚洲成人自拍视频| 久久躁狠狠躁夜夜爽| 亚洲欧美精品在线| 欧美在线视频观看| 久久精品女人| 亚洲激情综合| 亚洲黄色大片| 在线成人免费视频| 国产欧美日韩精品a在线观看| 欧美精品首页| 欧美sm重口味系列视频在线观看| 久久精品中文字幕一区二区三区| 欧美一区二区大片| 久久午夜国产精品| 欧美激情va永久在线播放| 欧美日韩精品久久久| 亚洲欧美日韩综合一区| 久久av一区二区| 欧美国产精品专区| 国产精品视频久久久| 韩日午夜在线资源一区二区| 国内久久精品视频| 国产情侣一区| 亚洲日韩中文字幕在线播放| 国产精品99久久久久久白浆小说| 久久精品国产精品| 亚洲欧洲一区二区天堂久久| 亚洲一级片在线观看| 另类国产ts人妖高潮视频| 欧美午夜精品伦理| 欧美精品三区| 在线欧美日韩| 久久国产精品久久w女人spa| 亚洲啪啪91| 欧美紧缚bdsm在线视频| 伊人久久婷婷| 欧美不卡视频一区发布| 久久国产婷婷国产香蕉| 国产精品永久免费| 在线日韩中文| 久久亚洲国产精品日日av夜夜| 亚洲天堂激情| 国产欧美日韩免费| 欧美一区在线看| 久久国产日韩欧美| 亚洲电影免费观看高清完整版在线| 久久久久久噜噜噜久久久精品| 欧美一区二区三区成人| 国产一区二区三区免费观看| 亚洲欧美日韩中文播放| 亚洲欧美综合网| 亚洲第一视频网站| 亚洲精品美女在线观看播放| 欧美日韩亚洲视频| 久久激情中文| 欧美日韩国产区一| 久久精品国产第一区二区三区| 欧美专区亚洲专区| 亚洲一区在线播放| 久久久久久久久一区二区| 久久亚洲精品一区二区| 亚洲视频在线观看三级| 欧美诱惑福利视频| 亚洲中无吗在线| 欧美刺激性大交免费视频| 午夜精品福利视频| 欧美激情在线播放| 久久一区亚洲| 国产性做久久久久久| 亚洲一区999| 女人香蕉久久**毛片精品| 午夜精品久久久久久久久久久久 | 久久噜噜噜精品国产亚洲综合| 欧美激情精品久久久久久大尺度| 久久精品成人一区二区三区蜜臀| 欧美日韩精品在线观看| 亚洲国产婷婷香蕉久久久久久99| 国产亚洲欧美一区| 久久精品日产第一区二区| 久久精品最新地址|