hdu 1443 + cug1015 约瑟夫问题变型

来源:互联网 发布:mac系统解锁打不开了 编辑:程序博客网 时间:2024/05/05 02:56

直接暴力会超时死的不能再死,然后用数组每次查找下一个被干掉的人,如果小于k,干掉。cug的不能用该方法做,会超时,因为数据量小,直接用hdu的方法就把前面的结果算出来保存起来,最后直接AC。

#include <iostream>#include <string>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstdio> using namespace std;const int maxn = 20 + 2 ;long long f[maxn];long long ans[maxn]; int main(){    int  k ;    memset(f , 0 , sizeof(f));    while(scanf("%d", &k )!= EOF && k)    {        if(f[k] != 0)        {            cout << f[k] << endl;            continue;        }        long long m = 1;        int n = 2 * k;        ////保证k轮不去掉好人        for(int i = 1 ; i <= k ;i ++ )        {            ans[i] = (ans[i-1] + m - 1) % ( n - i + 1);//下一个要去掉的人            if(ans[i] < k)      //如果去掉了好人            {                i = 0;//说明当前m值不合适,重新开始开始试验                m++;//间隔加1            }        }        f[k] = m;        cout << f[k] << endl;    }    return 0;}
//////////cug#include <iostream>#include <string>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 20  ;long long f[maxn] = {0 , 2 , 7 , 5 ,30 , 169 , 441 , 1872 , 7632 , 1740 , 93313 , 459901 , 1358657 ,                        2504881 , 13482720 , 25779600 , 68468401 , 610346880 , 1271932200 , 327280800 };int main(){    int k;    while(scanf("%d", &k )!= EOF && k)    {        cout << f[k] << endl;    }    return 0;}



0 0
原创粉丝点击