Dividing a Chocolate
64-bit integer IO format: %lld Java class name: Main
The boy and Karlsson are dividing a chocolate that the boy's parents have presented him for his birthday. A chocolate is a rectangle that consists of m × n chocolate squares, separated from each other by cutlines.
Of course, Karlsson does the dividing. First Karlsson divides the chocolate into two rectangular pieces, breaking it along some cutline. Since Karlsson wants to divide chocolate fairly, he would not be satisfied if the two pieces have different sizes. In this case he cuts away the piece equal to the smaller piece out of the larger piece, and eats it. If the pieces are still not equal, he does so again, and so on. After the pieces are finally equal, Karlsson eats on
Karlsson wants to make an initial cut in such a way that he would eat as much chocolate as possible. However, the boy knows that Karlsson likes chocolate very much, and he watches for the process closely. Karlsson must be very careful not to offend the boy. So he must not successively cut away and eat chocolate from the same piece --- this would seem too suspicious to the boy.
Help Karlsson to find out how much chocolate he can eat.
Input
There are mutiple cases in the input file.
Each case contains m and n (1 <= m, n <= 109 ).
There is an empty line after each case.
Output
Output on
Sample Input
6 5Sample Output
24In the example Karlsson must initially break a chocolate into pieces of sizes 3 × 6 and 2 × 6 . After that he can eat all chocolate but the boy's piece that would finally be 1 × 6 . Karlsson cannot, for example, cut a chocolate initially into pieces of sizes 5 × 5 and 1 × 5 --- after doing so, he would have to successively cut a piece away from the first on
Source
Tags
#include <iostream> #include <cstdio> #include <cstring> using namespace std; long long int fib[]={0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946, 17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465, 14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170, 1836311903}; long long int n,m; const int INF=0x3f3f3f3f; int main() { while(cin>>n>>m) { long long int kn=INF,km=INF; for(int i=3;fib<=n;i++) { if(n%fib==0) { kn=min(kn,n/fib); } } for(int i=3;fib<=m;i++) { if(m%fib==0) { km=min(km,m/fib); } } if(kn==INF&&km==INF) { puts("0"); putchar(10); } else { long long int ans=min(kn*m,km*n); printf("%lld ",m*n-ans); } } return 0; } |