zoukankan      html  css  js  c++  java
  • hdu 1443 Joseph (约瑟夫环)

    Joseph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2240    Accepted Submission(s): 1361


    Problem 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. 
     
    Sample Input
    3
    4
    0
     
    Sample Output
    5
    30

     比赛时候差一点就推出来了  自己比赛时太容易紧张了  尤其是跟队友一起时,唉  还是心态不行

    题意:总共有2*k个人站成一个环,前k个人是好人,后k个人是坏人,现在要处决这2*k个人要求后k个坏人要在好人之前死,让找一个最小的m(按照圆圈数m个人第m个人处死,然后从这个处死的人的下一位为起点继续数m个以此类推,直至杀死所有人)使得坏人先死

    题解:当前要处死的人为kill=(kill+m-1)%n   (n为当前活着的人的数量)如果kill等于0 证明是要处死第n个人所以kill=n 

    我直接用函数求解超时了  ,因为数据不大 所以我直接吧14个数打表输出了

    #include<stdio.h>
    #include<string.h>
    #include<cstdio> 
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 1100
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int f[20]; 
    int s[20]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720};
    int fun(int k,int m)
    {
    	int kill,i,j,n;
    	n=2*k;
    	kill=1;
    	for(i=1;i<=k;i++)
    	{
    		kill=(kill+m-1)%n;
    		if(kill==0)
    		    kill=n;
    		else if(kill<=k)
    		    return 0;
    		n--;
    	}
    	return 1;
    }
    int main()
    {
    	int n,m,j,i,t,k;
    	while(scanf("%d",&k),k)
    	{
    //		for(i=1;i<=14;i++)
    //		{
    //			for(j=1;;j++)
    //			{
    //				if(fun(i,j))
    //				{
    //					f[i]=j;
    //					break;
    //				}
    //			}
    //		}
    		printf("%d
    ",s[k]);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    如何创建自己的SAP CRM产品主数据search scenario
    互联网商业数据分析(六十九):数据可视化Tableau篇(九)其他知识点
    互联网商业数据分析(六十八):数据可视化Tableau篇(八)可视化项目(二)
    互联网商业数据分析(六十七):数据可视化Tableau篇(七)可视化项目(一)
    ubuntu关闭时间同步与centos更改时间
    对比Intel和Kunpeng+ openEuler
    pci config + resource
    IORESOURCE_MEM IORESOURCE_IO
    PCIE的mmio内存映射访问机制+ 配置空间 +mmap + resource + /dev/mem
    mmap PROT_READ | PROT_WRITE
  • 原文地址:https://www.cnblogs.com/tonghao/p/5329895.html
Copyright © 2011-2022 走看看