Additional Problems

  1. Shortest Subsequence 
  2. Counting Bits 
  3. Swap Game 
  4. Prufer Code 
  5. Acyclic Graph Edges 
  6. Strongly Connected Edges 
  7. Even Outdegree Edges 
  8. Multiplication Table 
  9. Advertisement
  10. Special Substrings 
  11. Permutation Inversions 
  12. Maximum Xor Subarray 
  13. Movie Festival Queries 
  14. Chess Tournament 
  15. Tree Traversals 
  16. Network Renovation 
  17. Graph Girth 
  18. Intersection Points 
  19. Inverse Inversions 
  20. Monotone Subsequences 
  21. String Reorder 
  22. Stack Weights 
  23. Pyramid Array 
  24. Increasing Subsequence II 
  25. String Removals 
  26. Bit Inversions 
  27. Xor Pyramid 
  28. Writing Numbers 
  29. String Transform 
  30. Letter Pair Move Game 
  31. Maximum Building I 
  32. Sorting Methods 
  33. Cyclic Array
  34. List of Sums 
  35. Increasing Array II 
  36. Food Division 
  37. Bit Problem 
  38. Swap Round Sorting 
  39. Binary Subsequences 
  40. Tree Isomorphism I 
  41. Counting Sequences 
  42. Critical Cities 
  43. School Excursion 
  44. Coin Grid 
  45. Robot Path 
  46. Programmers and Artists 
  47. Course Schedule II
  48. Removing Digits II 
  49. Coin Arrangement 
  50. Counting Bishops 
  51. Grid Puzzle I 
  52. Grid Puzzle II 
  53. Empty String 
  54. Grid Paths 
  55. Bit Substrings 
  56. Reversal Sorting 
  57. Counting Reorders 
  58. Book Shop II 
  59. Network Breakdown 
  60. Visiting Cities 
  61. Missing Coin Sum Queries 
  62. Number Grid 
  63. Maximum Building II 
  64. Filling Trominos 
  65. Stick Divisions 
  66. Coding Company 
  67. Flight Route Requests 
  68. Two Stacks Sorting 
  69. Tree Isomorphism II 
  70. Forbidden Cities 
  71. Area of Rectangles 
  72. Grid Completion 
  73. Creating Offices 
  74. Permutations II 
  75. Functional Graph Distribution 
  76. New Flight Routes 
  77. Grid Path Construction 

 

CSES Problem Set 소개 (link)

'CSES' 카테고리의 다른 글

CSES 10. Advanced Techniques  (0) 2021.07.21
CSES 9. Geometry  (0) 2021.07.21
CSES 8. String Algorithms  (0) 2021.07.21
CSES 6. Tree Algorithms  (0) 2021.07.21
CSES 5. Range Queries  (0) 2021.07.21

출처: https://train.usaco.org/usacogate


문제 설명

Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.

Here is the set when N = 5:

0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.

입력 양식

One line with a single integer N.

출력

One fraction per line, sorted in order of magnitude. 

입력 예 

5

출력 예

0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1 


문제 풀이 내용

받은 N보다 작은 정수들로 정수쌍 ( i, j ) 를 만들고 첫번째 수는 0, 마지막 수는 1/1 로 해서 vector에 저장한다. 이때 두수가 서로 서로소가 아닌 경우는 제외한다. 왜냐하면 1/2 = 2/4 = 3/6 ..이 되지만, 문제에서는 1/2만 출력해야 한다. vector를 정렬하는데, (a, b) vs (c, d)의 크기 비교는 a/b vs c/d => ad/bd vs bc/bd => ad vs bc 로 비교하도록 했다.

프로그램 내용

더보기
#define MAX_M 161

/// check co-prime a, b -> gcd(a, b) == 1
int CoPrime(int a, int b);

vector <pair<int, int>> sequence;

int main() {
    sequence.push_back(make_pair(0,1));

	for (int denominator = 1; denominator < M+1 ; ++denominator)
        for (int numerator  = 1; numerator < denominator ; ++numerator)
            if( CoPrime(numerator, denominator) == 1)
                sequence.push_back(make_pair(numerator, denominator));

	sequence.push_back(make_pair(1,1));

    sort(sequence.begin()+1, sequence.end()-1, compare2);

    for (int idx= 0; idx < sequence.size(); ++idx)
        fout << sequence[idx].first << "/" << sequence[idx].second << endl;

 

Chapter 2.  Bigger Challenges

'USACO Training' 카테고리의 다른 글

Problem 2.2.4 Subset Sums  (0) 2019.10.10
Problem 2.1.5 Sorting A Three-Valued Sequence  (0) 2019.09.18
Problem 2.2.5 Runaround Numbers  (0) 2019.09.18
Problem 1.6.4 SuperPrime Rib  (0) 2019.09.16
Problem 1.6.3 Prime Palindromes  (0) 2019.09.16

+ Recent posts