POJ1012

 摘要:枚舉的時候考慮剩下最後個 bad guy的時候,把圈打開排成隊,bad guy作爲最後一個。這時候要麼是從bad guy開始或從第一個開始,

從bad guy開始:m=t(k+1)+1 (t=1, 2, 3....)

從第一個開始:m=t(k+1) (t=1, 2, 3...)

例外在check每一個m的時候,每找到一個bad gun,就:

total--;

pos = pos-1;

if(pos==total-1){

  pos = 0;

}

求完所有的K(0<k<13)可以打表。

 

#include <iostream>
using namespace std;

const int size = 14;
int table[size] = {0, 2, 7, 5, 30, 169, 441, 1872, 7632, 1740, 93313, 459901, 1358657, 2504881};

int main()
{
    int k;
    while(cin >> k){
        if( k == 0 ){
            break;   
        }
        cout << table[k] << endl;   
        /*
        if( table[k] != 0 ){
            cout << table[k] << endl;
            continue;
        }
        for(int t=1; ;t++){
            int i = (k+1)*t;   
           
            int pos = 0;
            int rest = 2 * k;
            for(int num=1; num<=k; num++){
                pos=(pos+i-1)%rest;
                if(pos < k){
                    break;
                }
                rest--;
                if(pos == rest){
                    pos = 0;
                }
            }

            if( rest == k ){
                cout << i << endl;
                table[k] = i;
                break;       
            }
           
            i = (k+1)*t + 1;   
            pos = 0;
            rest = 2 * k;
            for(int num=1; num<=k; num++){
                pos=(pos+i-1)%rest;
                if(pos < k){
                    break;
                }
                rest--;
                if(pos == rest){
                    pos = 0;
                }
            }

            if( rest == k ){
                cout << i << endl;
                table[k] = i;
                break;       
            }
        }
        */   
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章