The first method of the Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to N.The Sieve of Eratosthenes uses an extra O (n) memory and their runtime complexity is O (n log log n )Creates a Boolean array of length = N IsPrime, with each element initialized to true;K = 2: (n-1), if the current number k is the prime, put K^k-(n-1)/k *k of the number of pairs of mappings = false;Finally, count from 2 to n-1 IsPrime = = True.Code:public class Solution {public int c
Test instructions: Finding the smallest dominating set on a treeMinimum dominating set: The point set, that is, each point can be "dominated" to an adjacent point, and the minimum number of points can be used to control all points.The minimum dominating set on the graph is NP, but the tree can be done by DP, which is O (n).It's good to be violent.F[i] Indicates the minimum cost of full domination of the subtree at the point when it is selectedG[i] Indicates the minimum cost of full domination of
Iterate through an array to find out if there are any duplicate elements first think of the number of elements that appear with an array recordCode:public class Solution {public boolean containsduplicate (int[] nums) {if (Nums.length = = 0) return true;int[] checkelement = new Int[integer.max_value];for (int i = 0; i if (Checkelement[nums[i]] > 0) return false;checkelement[nums[i]]++;}return true;}}Space cost too big: Requested array size exceed VM limitUse HashMap to do it:public class Solution
DescriptionFJ ' s n (1 There are N (1 Input* Line 1:four space-separated integers:n, I, H and R* Lines 2..r+1:two distinct space-separated integers A and B (1 Output* Lines 1..n:line I contains the maximum possible height of cow I.Sample Input9 3 5 51 35 34 33 79 8INPUT DETAILS:There is 9 cows, and the 3rd was the tallest with height 5.Sample Output545344555HINTExercisesToday ask what is a differential sequence but this visual is not a knowledge point. It is good to use the difference sequence t
The main topic: given a picture, starting from 1 to go to the end of the last 1, there is an opportunity to go backward along a certain side, ask the most can pass the number of pointsObviously, if there's no chance of a reversal, the answer is the size of the strongly connected component of number 1th.Now that's the chance. The graph after which an edge is reversed to a point is formed by a loop that contains the strong connected component of the number 1th node in order to increase the answerD
1596: [Usaco2008 Jan] Telephone networkDescriptionFarmer John has decided to equip all his cows with mobile phones to encourage them to communicate with each other. For this reason, however, FJ had to select a radio tower in the N (1 Input* Line 1th: 1 integers, N* 2nd. N rows: 2 integers separated by spaces A, B, for two adjacent grassland numbersOutput* Line 1th: output 1 integers, i.e. the number of radio communication towers established at least F
4506: [Usaco2016 jan]fort MooDescriptionBessie is building a fort with her friend Elsie. Like any good fort, this one needs to start with a sturdy frame. Bessie wants to build a frame in the shape of a one-meter-wide rectangular outline, atop which she'll build the fort.Bessie have already chosen a site on which to build the fort-a piece of land measuring NN meters by MM meters (1InputLine 1 contains integers N and M.the next N lines each contain M ch
1699: [Usaco2007 jan]balanced lineup queue time
limit:5 Sec Memory limit:64 MB
DescriptionEvery day, farmer John's N (1 Input* First line: N and Q. * 2nd. N+1: Line i+1 is the height of the first cow.* Line n+2..n+q+1: Two integers, A and B (1 Output* 1th. Q Line: All inquiries answered (the highest and lowest of the cow's height), one per line.Sample Input6 31734251 54 62 2Sample Output630HINT SourceGoldTest instructions: Interval maximum-in
bzoj1677[usaco2005 Jan]sumsets SummationTest instructionsGives an N, using some 2 of the number of powers of the sum to seek it. Ask how many ways. n≤1000000. ExercisesYou can write a complete backpack, but the ranking will be the countdown ~ Positive solution is a recursive type:F[i]=f[i-1],i is odd, f[1]=1F[i-1]+f[i/2],i to evenThe proof of God's Ben I didn't read Qaq.Code:1#include 2#include 3#include 4 #defineInc (I,J,K) for (int i=j;i5 #defineMAX
Test instructionsGive a directed graph and choose a path with a path starting point of 1, and one chance to go in the opposite direction of a certain edge, and ask how many points can be passed? (a point in the path, regardless of how many positive integer number of contributions to the answer are 1)= = Map We will consider the point of contraction first. Then observe the graph after the indentation to find that the new path must have only one edge reversed to meet the criteria. Then we can thin
Bzoj1613[usaco2007 Jan]running Bessie's morning exercise planTest instructionsBessie ran for an n-minute morning run. At the beginning of every minute, Bessie chooses whether the next minute is for running or resting, and if Bessie chooses to run in the first minute, she can run di in that minute, and her fatigue will increase by 1. Bessie's upper limit of fatigue is m. If Bessie chooses to rest, her fatigue will be reduced by 1 per minute, but she ha
bzoj4525: [Usaco2016 jan]angry cows time
limit:1 Sec Memory limit:128 MBSubmit:90 solved:71[Submit] [Status] [Discuss]
Description Cow Bessie designed a game: "Angry cows". The prototype of the game is: there are some explosive haystack distributed in some coordinates of a number axis, play a home slingshot to launch a cow to the number line. The shock wave of a cow hitting the axis will cause a nearby haystack to explode.The goal of the game
4509: [Usaco2016 jan]angry cows time
limit: Sec
Memory Limit: ten MB
Submit:
Solved: 38
[Submit] [Status] [Discuss]
Descriptionbessie The cow have designed what she thinks'll be is the next big hit video game: "Angry cows". The premise, which she believes is completely original, was that the player shoots a cow with a slingshot into a one-dimens Ional scene consisting of a set of hay bales located at various points on a num
BZOJ1596[USACO2008 Jan] Telephone networkTest instructionsSelect the smallest point in a tree to build a tower, so that each point has a tower or adjacent point. n≤10000.ExercisesGreedy. DFS builds a tower at its father, for each current point, after Dfs finishes all its child nodes if it as well as its son and its father does not have a tower.Code:1#include 2#include 3#include 4#include 5 #defineInc (I,J,K) for (int i=j;i6 #defineMAXN 100107 using na
1634: [Usaco2007 jan]protecting the Flowers flower time limit:5 Sec Memory limit:64 MB
submit:493 solved:310
[Submit] [Status]
Description Farmer John went to cut some wood and left N (2 John left his n cows up to the mountain to gather wood. When he left, they were leisurely grazing in the meadow as usual. But when he came back, he saw a scene of tragedy: The cows were hiding in his garden, eating the beautiful flowers he loved!To minimize
3433: [Usaco2014 jan]recording the Moolympicstime limit:10 Sec Memory limit:128 MB
submit:137 solved:89
[Submit] [Status] [Discuss]
DescriptionBeing a fan of all cold-weather sports (especially those involving cows), Farmer John wants to record as much of the upcom ing winter moolympics as possible. The television schedule for the moolympics consists of n different programs (1 Give n intervals [a, b]. There are 2 loggers. The intervals store
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.