HDU 1795 the least one

Source: Internet
Author: User

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 Descriptionin the RPG game ' go back Ice Age ' (I decide to develop the game after my undergraduate education), all Heros has the IR own respected value, and the skill of killing monsters is defined as the following Rule:one hero can kill the Monstrer s whose respected values is smaller then himself and the both respected values have 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 has to kill at least M ones. To minimize the damage of the battle, you should dispatch a hero with minimal respected value. Which hero would you dispatch? There is 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 ') is positive. 
InputThe first line has one integer q and then Q lines follow. In the Q lines there are an integer Mi, 0<q<=1000000, 0<mi<=10000.
 
OutputFor each case , there is Q results, in each result, you should output the value of the hero you'll dispatch to COM Plete the task. 
Sample Input
237
 
Sample Output
511
 
AuthorWangye 
Source"Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (4)Test instructions: There is a game in which the heroes of the game can comparehe kills a monster with less energy.Andthere is no common factor between the energy of this monster and the energy of the hero.Anda hero can kill at least the number of the input monsters ' energy values .Beast,That is, you can kill a monster that is less than or equal to the energy value of the input monster, and because it requires no common factor between the energy of the beast and the energy of the hero, so the hero's energy value must be a prime number, and the energy value of the Beastbe BIG! Note: The energy value of a monster can not be a prime number! Idea: This problem is more difficult to read the question, I understand test instructions spent a night of time. But after understanding, it is better to do the problem, in fact, the abstract mathematical model of the problem is to let the input of a number, so that the number is larger than the size of a prime (the prime number is larger than the smallest of the primes), this is relatively simple, we can use a more stupid method,  is to find the prime number from small to large in the value greater than the input, if found on the output and jump out of the loop; we can also do it with a table and a traversal; we can do it by using the dichotomy method. These three methods are all possible! Method One: Code:
/* Time: 3000MS time Limit exceeded modified to: 2745MS by!!!! */#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 + +)//is originally for (j=2;j<i;j++) {if ( i%j==0) break   ;} if (j> (int) sqrt (i*1.0))//The original is: if (j>=i)    {     printf ("%d\n", I); break;   

Method two relatively simple will not write! Method Three: Code:
#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;}

Just learn two points, will look for the function to write a bit, experience a bit of the process of two points, the specific code is as follows:
#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;// This part can be replaced by the function J=upper_bound (a,a+k,m)-a;a[j]!     while (left<=right) {mid= (left+right)/2;if (a[mid]<=m) left=mid+1;elseright=mid-1;     } printf ("%d\n", A[left]);//In this case, if using a[ringt] will be equal to that value, if it is a[left] will be more than that worth the minimum value! }}

There is also a code to hit the table (mainly this way to hit the table relatively fast) plus traversal, this method is the fastest, so also to master, the code is as follows:
/* How to play the table!!!!!!!!! Spents: 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;}

 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1795 the least one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.