160 likes | 446 Views
(SGU) 231 . Prime Sum time limit per test: 0.5 sec. memory limit per test: 4096 KB Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N. Input The input of the problem consists of the only integer N (1<=N<=10^6). Output
E N D
(SGU) 231. Prime Sum time limit per test: 0.5 sec. memory limit per test: 4096 KB Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N. Input The input of the problem consists of the only integer N (1<=N<=10^6). Output On the first line of the output file write the number of pairs meeting the requirements. Then output all pairs one per line (two primes separated by a space). Sample test(s) Input 4 Output 0 Author: Antony Popovich Resource: Leningrad Regional School Programming Olympiad Date: January 9, 2004 http://acm.sgu.ru/problem.php?contest=0&problem=231
#include <iostream> //Accepted 46 ms 1919 kb #include <cmath> #include <cstring> using namespace std; constint LEN=1000008; bool p[LEN]; intn,cnt; int main(){ memset(p,1,sizeof(p)); for(inti=2;i<=sqrt((double)LEN);i++) if(p[i]) for(int j=2;j<=LEN/i;j++) p[i*j]=0; cin>>n; for(inti=3;i+2<=n;i+=2) if(p[i]&&p[i+2]) cnt++; cout<<cnt<<endl; for(inti=3;i+2<=n;i+=2) if(p[i]&&p[i+2]) cout<<2<<" "<<i<<endl; return 0; }
(soj)1422. Table Tennis Time Limit: 1sec Memory Limit:32MB Description There is a rectangular pool table ABCD with side lengths m and n, where m and n are integers with m<n. Bug put the cue ball at corner A and shoot it at a 45 angle to the sides, and it reflects perfectly (at 45 degres) off all sides, and never loses energy. The table has four pockets, one in each corner, and the ball will be sunk as soon as it hits a corner. The question is : given the integers m and n, how do you tell which pocket the ball hits first, and how many reflections will the ball make on the way? Assume A,B,C,D is the lower left corner,upper left corner, upper right corner, lower right corner respectively. The length of AB is m, and the length of AD is n. B---------C | | | | A---------D
Input Input consist of mutiple cases. Each case include two integers m and n, both of which fits in 32-bit signed integer. Output For each case, you should find out which pocket (A,B,C,D) the ball first hit and how many reflections the ball make . Sample Input 6 10 Sample Output C 6 http://soj.me/1422
#include <iostream> #include <cstdio> using namespace std; int lcm(intx,int y) { //omitted} int main() { inta,b,c,p,q,ans; while( scanf("%d%d",&a,&b) != EOF ) { c=lcm(a,b); p=c/a; q=c/b; ans=p+q-2; if(p%2&&q%2) cout<<"C "; else if(p%2&&q%2==0) cout<<"B "; else if(p%2==0&&q%2) cout<<"D "; else cout<<"A "; cout<<ans<<endl; } return 0; }
#include <iostream> //AC #include <cstdio> using namespace std; intgcd( inta,int b ) { return (b!=0)?gcd(b,a%b):a; } int main() { int a, b; while( scanf("%d%d",&a,&b) != EOF ) { int c = gcd( a, b ); int k1 = b / c, k2 = a / c; int result = k1 + k2 - 2; if( k1 % 2 && k2 % 2 ) printf("C "); else if( k1 % 2 && ! (k2%2) ) printf("B "); else if( !(k1%2) && k2 % 2 ) printf("D "); else printf("A "); printf("%d\n",result); } return 0; }
1006. Team Rankings Time Limit: 1sec Memory Limit:32MB Description It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets, the Cats, the Dribblers, and the Elephants. When Scoop McGee, sports editor of the paper, gets the rankings from the selected local experts down at the hardware store, he's dismayed to find that there doesn't appear to be total agreement and so he's wondering what ranking to publish that would most accurately reflect the rankings he got from the experts. He’s found that finding the median ranking from among all possible rankings is one way to go. The median ranking is computed as follows: Given any two rankings, for instance ACDBE and ABCDE, the distance between the two rankings is defined as the total number of pairs of teams that are given different relative orderings. In our example, the pair B, C is given a different ordering by the two rankings. (The first ranking has C above B while the second ranking has the opposite.) The only other pair that the two rankings disagree on is B, D; thus, the distance between these two rankings is 2. The median ranking of a set of rankings is that ranking whose sum of distances to all the given rankings is minimal. (Note we could have more than one median ranking.) The median ranking may or may not be one of the given rankings.
Suppose there are 4 voters that have given the rankings: ABDCE, BACDE, ABCED and ACBDE. Consider two candidate median rankings ABCDE and CDEAB. The sum of distances from the ranking ABCDE to the four voted rankings is 1 + 1 + 1 + 1 = 4. We'll call this sum the value of the ranking ABCDE. The value of the ranking CDEAB is 7 + 7 + 7 + 5 = 26. It turns out that ABCDE is in fact the median ranking with a value of 4. Input There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input. Output Output for each input set should be one line of the form: ranking is the median ranking with value value. Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically. http://soj.me/1006
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int n; //Number of Rankings int min; //Record the index of the answer string temp; string ranking[120]; //Record all the permutations string s ="ABCDE"; ranking[0] = s; for (inti=1; next_permutation(s.begin(),s.end()); i++ ) ranking[i] = s; while ( cin >> n && n )
intvalue[120]={}; while(n--) { cin>>temp; for(inti=0;i<120;i++) for (int j=0;j<5;j++) for (int k=j+1; k<5;k++) if (temp.find(ranking[i][j])>temp.find(ranking[i][k])) value[i]++; } min=0; for(inti=1;i<120;i++) if (value[min]>value[i]) min=i; cout<<ranking[ min ]<<" is the median ranking with value "<< value[min]<<".\n"; } return 0; }
(Ural) 1234. Bricks Time Limit: 1.0 second Memory Limit: 16 MB The prisoner of the "IF" castle has decided to run away by disassembling the brick wall in his prison cell. To hide his work from his jailors he shall get rid of the bricks that he removes from the wall. All bricks have a shape of rectangular parallelepiped with the size of A × B × C inches and are so strong that they are impossible to break. However, there's a small rectangular sewer hole in the cell's floor with the size of D × E inches that goes deep down as a rectangular well of the same size (so deep it is, that its depth could not be measured and can be neglected). The prisoner have precisely (up to a tenth of an inch!) measured all the sizes A, B, C, D, E and wants to know if it is possible to dispose of the castle's bricks through the hole in the floor. Please, answer this question for him.
Input The only line contains numbers A, B, C, D, and E separated by spaces. A, B, C are the lengths of brick's sides, and D, E are the lengths of hole's sides. All lengths are at least 1 and at most 10 inches and have at most 1 digit after decimal point. Output Write a single word YES if it is possible to dispose of the bricks through the hole or NO otherwise. Samples Problem Author: Elena Andreeva Problem Source: 2002-2003 ACM Northeastern European Regional Programming Contest http://acm.timus.ru/problem.aspx?space=1&num=1234
#include <iostream> #include<cmath> using namespace std; void Swap(double & a,double & b) { double tmp=a;a=b;b=tmp; } bool check(double c,doubled,doublea,double b) { if(a*b<c*d)return false; if(a<b)Swap(a,b); if(c<d)Swap(c,d); if(b<d)return false; if(a>=c && b>=d)return true; double dis=sqrt(c*c+d*d); double gamma=asin(b/dis); double beta=asin(d/dis); double alpha=gamma-beta; double Len=c*cos(alpha)+d*sin(alpha); if(Len<=a)return true; return false; } int main() { double l,m,n,o,p; cin>>l>>m>>n>>o>>p; if(check(l,m,o,p)||check(l,n,o,p)||check(m,n,o,p)) cout<<"YES"<<endl; else cout<<"NO"<<endl; system("pause"); return 0; }