HDU 1795 The least one

The least one

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 535    Accepted Submission(s): 204

Problem Description

In the RPG game “go back ice age”(I decide to develop the game after my undergraduate education), all heros have their own respected value, and the skill of killing monsters is defined as the following
rule: one hero can kill the monstrers whose respected values are smaller then himself and the two respected values has none common factor but 1, so the skill is the same as the number of the monsters he can kill. Now each kind of value of the monsters come.
And your hero have to kill at least M ones. To minimize the damage of the battle, you should dispatch a hero with minimal respected value. Which hero will you dispatch ? There are Q battles, in each battle, for i from 1 to Q, and your hero should kill Mi ones
at least. You have all kind of heros with different respected values, and the values(heros’ and monsters’) are positive.

Input

The first line has one integer Q, then Q lines follow. In the Q lines there is an integer Mi, 0<Q<=1000000, 0<Mi<=10000.

Output

For each case, there are Q results, in each result, you should output the value of the hero you will dispatch to complete the task.

Sample Input

2
3
7

Sample Output

5
11

Author

wangye

Source

2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)

题意:

有一个游戏,游戏中的英雄能够将比他能量少的怪兽杀掉并且要求这个怪兽的能量与英雄的能量之间没有公因数,而且一个英雄至少可以杀掉输入的怪兽的能量值的个数的怪

兽,也就是能将小于等于输入怪兽能量值小的怪兽全杀掉,又因为要求要求这个怪兽的能量与英雄的能量之间没有公因数,所以要求英雄的能量值必须是素数,并且比怪兽的能量值

要大!注:怪兽的能量值可以不是素数!

思路:

这道题比较难读懂题,我弄懂题意就花了一晚上的时间。但是理解了之后,就比较好做题了;其实这道题抽象出来的数学模型,就是让输入一个数,让求比这个数大的一个素数(这个素数是比那个数大的素数中的最小值),这个就比较简单了,我们可以用比较笨的方法,就是在大于输入的值中从小到大找素数,如果找到就输出并跳出循环;我们也可以用打表和遍历的方法做;我们还可以用二分法做;这三种方法都可以!

方法一:

代码:

/*
时间:3000MS
 Time Limit Exceeded
 修改后时间变成 : 2745MS
 通过!!!!
*/
#include <stdio.h>
#include <math.h>
int a[10100];
int main()
{
	int n,m,i,j,k;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		for(i=m+1;i<10100;i++)
		{
			for(j=2;j<=(int)sqrt(i*1.0);j++)//原来是 for(j=2;j<i;j++)
			{
				if(i%j==0)
				   break;
			}
			if(j>(int)sqrt(i*1.0))//原来是:if(j>=i)
			   {
			     printf("%d\n",i);
				 break;
			   }
		}
	}
	return 0;
} 

方法二比较简单就不写了!

方法三:

代码:

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int a[10100],k;

void sushu()
{
	k=0;
	for(int i=2;i<10100;i++)
		{
			int w=0;
		 for(int j=2;j<=(int)sqrt(i*1.0);j++)
		{
			if(i%j==0)
				w=1;
		}
		if(w==0)
			a[k++]=i;
		}
}

int main()
{
	int n,m,i,j;
	sushu();
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		j=upper_bound(a,a+k,m)-a;
		printf("%d\n",a[j]);
	}
	return 0;
}

刚学二分,将寻找的函数自己动手写了一下,体会了一下二分的过程,具体代码如下:

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int a[10100],b[10100],k;

void sushu()
{
	k=0;
	for(int i=2;i<10100;i++)
		{
			int w=0;
		 for(int j=2;j<=(int)sqrt(i*1.0);j++)
		{
			if(i%j==0)
				w=1;
		}
		if(w==0)
			a[k++]=i;
		}
}

int main()
{
	int n,m,i,j;
	sushu();
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		int left=0,right=k-1,mid;//这一部分完全可以用函数j=upper_bound(a,a+k,m)-a;a[j]来代替!
    	while(left<=right)
		{
			mid=(left+right)/2;
			if(a[mid]<=m)	left=mid+1;
			else	right=mid-1;
	    }
		printf("%d\n",a[left]);//这里面,如果用a[ringt]会是等于那个值,如果是a[left]则会取到大于那个值得最小值!
	}
}

还有一个代码是打表(主要是这种打表方法比较快)加遍历,这个方法最快,所以也要掌握,代码如下:

/*
打表的方法!!!!!!!!!
用时:780MS
*/
#include <stdio.h>
int a[10100];
int main()
{
	int i,j,k,n,m,t;
	a[1]=1;
	for(i=2;i<10100/i;i++)
		for(j=2;j<10100/i;j++)
		{
			if(a[i]==0)
			{
				a[i*j]=1;
			}
		}
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		for(i=m+1,t=0;i<10100;i++)
		{
			if(a[i]==0)
			   {
			      break;
			  }
		}
		printf("%d\n",i);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 19:06:35

HDU 1795 The least one的相关文章

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 1798 Tell me the area (计算几何)

Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1801    Accepted Submission(s): 542 Problem Description There are two circles in the plane (shown in the below picture), there is

HDU 1796 How many integers can you find(组合数学-容斥原理)

How many integers can you find Problem Description Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer

HDU 4081 Qin Shi Huang&#39;s National Road System(prim)

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5153    Accepted Submission(s): 1795 Problem Description During the Warring States Period of ancient China(4

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是