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

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) 評(píng)論(3)  編輯 收藏 引用 所屬分類: POJ

約瑟夫環(huán)實(shí)在是太奇妙啦(我很高興我的這篇原創(chuàng)文章被不少人轉(zhuǎn)載了,雖然他們都沒有引用出處... ...)!
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;
} // 這是從網(wǎng)上找的做法,號(hào)稱打表法,這些數(shù)據(jù)依舊要通過建立循環(huán)鏈表或是別的模擬法來求出。但是單純?yōu)榱薃C,這種做法真的是相當(dāng)有效,講白了就是有目的的窮舉結(jié)果。
1012模擬法 C: 可惜呀可惜!這個(gè)總是 超時(shí)!我不知道是什么原因。但是思路是正確的,可能有些地方我沒有考慮到,看到這篇日志的人請(qǐng)指點(diǎn)。
#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;
}
對(duì)于 2244,建議看一個(gè)牛人的ACM博客: m.shnenglu.com/AClayton/archive/2007/11/06/35964.html
我就是看這篇博文的,很牛的一個(gè)人 AClayton ,寫的日期剛好是我生日,下面是其全部博文:
--------------------------------------------------------------------------------------------------------------------------------------------

 在沒有明白約瑟夫問題之前,只能用模擬來做.
      約瑟夫問題是這樣的:
      假設(shè)n個(gè)人,編號(hào)為1到n,排成一個(gè)圈,順時(shí)針從1開始數(shù)字m,數(shù)到m的人殺了,剩下的人繼續(xù)游戲.活到最后的一個(gè)人是勝利者.一般來說需要編程求解最后一個(gè)人的編號(hào).
      思路是這樣的:
     假設(shè)當(dāng)前剩下i個(gè)人(i<=n),顯然這一輪m要掛(因?yàn)榭偸菑?開始數(shù)).經(jīng)過這一輪,剩下的人是:1 2 3 ... m- 1 m + 1 ... i, 我們將從m+1開始的數(shù)映射成1, 則m+2對(duì)應(yīng)2, n對(duì)應(yīng)i - m, 1對(duì)應(yīng)成i - m + 1  m - 1對(duì)應(yīng)i - 1,那么現(xiàn)在的問題變成了已知i - 1個(gè)人進(jìn)行循環(huán)報(bào)數(shù)m,求出去的人的序號(hào)。假設(shè)已經(jīng)求出了i- 1個(gè)人循環(huán)報(bào)數(shù)下最后一個(gè)出去的人的序號(hào)X0,那么它在n個(gè)人中的序號(hào)X1=(X0+ m - 1) % n + 1,  最初的X0=1 ,反復(fù)迭代X0和X1可以求出.
     簡(jiǎn)單約瑟夫問題的解法:
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}
  

      這倒題其實(shí)不是完全的約瑟夫問題,而是稍微變了形.呵呵,聰明的讀者自己發(fā)現(xiàn)!這一點(diǎn)費(fèi)了我很久的時(shí)間,還害我逃了課被點(diǎn)名...
    這道題的我解法是這樣的.

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

       讀一個(gè)數(shù)處理一個(gè)數(shù), Memory 68K,時(shí)間31MS,如果覺得效率不高. 優(yōu)化的辦法是打表~

-----------------------------------------------------------------------------------------------------------------------------
由此可見,將問題化歸為數(shù)學(xué)問題,用初等高等或是數(shù)論來解決的能力是多么重要。
下面是我根據(jù)AClayton的思路簡(jiǎn)化后的代碼,可直接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 問題詳解  回復(fù)  更多評(píng)論   

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

# re: POJ 1012 2244 Joseph 問題詳解[未登錄]  回復(fù)  更多評(píng)論   

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

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

# re: POJ 1012 2244 Joseph 問題詳解  回復(fù)  更多評(píng)論   

2014-03-13 21:54 by shang
第一題的超時(shí)是因?yàn)?輸入數(shù)據(jù)有很多組 你要是每次都算的話就會(huì)超 所以要先打表存起來 O(1)詢問就不會(huì)超了
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产美女搞久久| 亚洲在线播放| 亚洲一区二区动漫| 亚洲三级电影在线观看| 亚洲人精品午夜在线观看| 亚洲日本aⅴ片在线观看香蕉| **性色生活片久久毛片| 亚洲国产三级网| 亚洲美女电影在线| 亚洲专区在线| 久久免费99精品久久久久久| 免费久久99精品国产自在现线| 亚洲高清精品中出| 一区二区免费在线视频| 午夜精品久久久99热福利| 久久久久久久91| 欧美激情在线| 国产日本欧美一区二区| 亚洲高清不卡在线观看| 亚洲天堂av电影| 久久色在线观看| 日韩视频免费观看高清完整版| 亚洲小视频在线| 久久午夜视频| 国产精品日日摸夜夜摸av| 永久免费毛片在线播放不卡| 亚洲天堂成人| 欧美成人午夜77777| 一区二区三区国产精品| 久久av免费一区| 欧美视频专区一二在线观看| 伊人久久大香线| 香蕉免费一区二区三区在线观看| 欧美大片第1页| 羞羞视频在线观看欧美| 欧美日韩国产二区| 亚洲国产精品久久91精品| 欧美怡红院视频| 亚洲美女视频在线观看| 久久久久国产精品人| 欧美一区二区三区久久精品| 国产日本精品| 一区二区三区日韩欧美精品| 老牛影视一区二区三区| 亚洲一区三区电影在线观看| 欧美精品国产一区| 在线观看日韩国产| 久久久久久国产精品mv| 亚洲线精品一区二区三区八戒| 欧美激情一区二区三区四区| 激情亚洲网站| 久久激情久久| 亚洲欧美一区二区三区久久| 国产精品激情偷乱一区二区∴| av成人免费| 亚洲日产国产精品| 欧美激情精品久久久久久| 亚洲国产精品电影| 美女免费视频一区| 久久久久亚洲综合| 136国产福利精品导航网址| 久久久久久久久久久成人| 亚洲欧美日韩在线| 国产色综合网| 久久综合网络一区二区| 久久九九热re6这里有精品| 激情成人av在线| 免费久久99精品国产| 欧美~级网站不卡| 亚洲精品自在在线观看| 亚洲精品国产精品国自产在线 | 欧美一区二区黄色| 亚洲欧美日韩专区| 国产中文一区二区| 免费久久久一本精品久久区| 麻豆成人综合网| 亚洲精品午夜| 一本色道久久精品| 国产日韩欧美在线播放| 久久综合伊人77777麻豆| 久久综合影视| aaa亚洲精品一二三区| 亚洲天天影视| 国产综合视频在线观看| 欧美成人自拍| 欧美午夜精品久久久久久久 | 国产精品99久久久久久人| 日韩午夜三级在线| 国产区在线观看成人精品| 欧美大片在线观看| 欧美天天在线| 久久综合中文| 欧美日韩一区二区视频在线| 久久国产精品一区二区| 免费成人性网站| 香蕉久久国产| 美女视频网站黄色亚洲| 亚洲免费一区二区| 中文日韩在线视频| 99re66热这里只有精品4| 欧美日韩在线视频观看| 欧美亚洲视频在线看网址| 久久一区视频| 亚洲一二三区在线观看| 久久久免费观看视频| 亚洲夜间福利| 久久亚洲精品一区二区| 亚洲欧美日韩成人高清在线一区| 久久美女性网| 欧美一区二区视频97| 欧美国产1区2区| 久久免费视频在线观看| 欧美性猛交一区二区三区精品| 欧美 日韩 国产 一区| 国产精品国产三级国产普通话三级| 美女91精品| 国产欧美视频一区二区| 99热这里只有成人精品国产| 在线观看亚洲视频啊啊啊啊| 亚洲男人第一网站| 一区二区三区四区五区精品| 久热国产精品视频| 久久香蕉国产线看观看网| 国产精品人人做人人爽人人添| 最新日韩精品| 在线精品福利| 久久精品30| 久久久亚洲欧洲日产国码αv | 欧美好骚综合网| 免费观看久久久4p| 国产一区二区三区黄| 亚洲欧美日韩国产成人| 亚洲一区国产一区| 欧美巨乳波霸| 亚洲激情在线激情| 亚洲日韩欧美视频一区| 男女精品网站| 亚洲国产精品va在线观看黑人| 亚洲高清视频中文字幕| 久久综合久久综合这里只有精品| 久久影院午夜论| 在线电影一区| 美女在线一区二区| 亚洲二区免费| 一区二区三区国产在线| 欧美日韩在线一区| 在线亚洲国产精品网站| 先锋影音久久久| 国产在线国偷精品产拍免费yy| 欧美一区二区视频在线| 久久婷婷人人澡人人喊人人爽 | 麻豆成人在线| 亚洲区在线播放| 亚洲一区国产| 国产精品自拍三区| 久久精品视频免费播放| 久久久久久尹人网香蕉| 亚洲福利在线观看| 欧美精品久久久久久久| 制服丝袜亚洲播放| 久久国产一区| 亚洲欧洲在线免费| 国产日韩欧美夫妻视频在线观看| 欧美午夜一区二区| 亚洲一区二区三区免费在线观看 | 免播放器亚洲| 亚洲精选久久| 欧美一级片久久久久久久| 激情综合亚洲| 欧美激情一区二区三区四区 | 欧美高清视频一区二区| 一区二区三区精品久久久| 久久成人18免费观看| 亚洲人成毛片在线播放| 国产精品久久夜| 乱中年女人伦av一区二区| 99视频精品在线| 免费成人性网站| 亚洲欧美日韩一区二区在线| 极品av少妇一区二区| 欧美小视频在线观看| 久久久蜜桃一区二区人| 亚洲视频在线看| 欧美电影在线观看| 久久激情视频| 亚洲欧美国内爽妇网| 亚洲韩国青草视频| 国产区二精品视| 欧美日韩一区国产| 欧美v日韩v国产v| 欧美在线啊v| 亚洲视频中文字幕| 亚洲激情女人| 女女同性女同一区二区三区91| 亚洲在线视频免费观看| 亚洲精品乱码久久久久久| 狠狠色综合色区| 国产日韩欧美三级| 国产精品久久久久影院色老大 | 午夜电影亚洲|