[TIOJ] 1004. 猶太人敢死隊問題

題目連結:http://tioj.infor.org/problems/1002
不難發現本題的規律,也就是對於f(n)他是個遞增的奇數數列,而當n為2的冪次時,f(n)重新從1開始遞增
嚴謹證明請見維基百科https://zh.wikipedia.org/wiki/约瑟夫斯問題
#include <stdio.h>
int main(){
        int a,b=1;
        scanf("%d",&a);
        while(a>=b*2){
                b*=2;
        }
        printf("%d\n",(a-b)*2+1);
        return 0;
}

留言

這個網誌中的熱門文章

[TIOJ] 1902. 「殿仁.王,不認識,誰啊?」,然後他就死了……

[AtCoder] [經典競程 90 題] 024 - Select +/- One(★2)

[AtCoder] [經典競程 90 題] 022 - Cubic Cake(★2)