HDU 1722 Cake (GCD)

Source: Internet
Author: User

HDU 1722 Cake (GCD)

Cake
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2609 Accepted Submission (s): 1253
Problem Description a birthday Party may contain p or q people. A big cake is prepared. ask how many pieces of cake should be cut into at least (each piece is not necessarily the same size) so that the cake can be evenly divided in any situation where p or q people are present.
Each Input row has two numbers p and q.
Output
The minimum number of pieces of cake to be cut.
Sample Input
2 3
Sample Output
4HintCut the cake into 1/3, 1/3, 1/6, and 1/6 pieces to meet the requirements. When two people come, each person can eat 1/3 + 1/6 = 1/2, 1/2 pieces. When three people come, each person can eat 1/6 + 1/6 = 1/3, 1/3, 1/3.
AuthorLL
SourceHZIEE 2007 Programming Contest




Solution: first, make a p block, and then combine it. Then, from the original place, divide the cake into q parts, and the number of completely overlapped parts will surely appear in the middle, the number of blocks to be split is p + q-k.

Now, you only need to find k. Actually, what is k, that is, GCD (p, q), which is the maximum approximate number of p and q.




AC code:

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     #include 
     
      #include 
      
       #include 
       #include 
        
         #include 
         
          #include 
          
           #include 
           
            using namespace std;#define INF 0x7fffffffint gcd(int a, int b){ return !b ? a : gcd(b, a%b);}int main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int p, q; while(scanf("%d%d",&p, &q)!=EOF) { printf("%d\n", p + q - gcd(p, q)); } return 0;}
           
          
         
        
      
     
    
   
  
 





Related Article

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.