CODEFORCES 1181A

CODEFORCES

1181A - Chunga Changa

Link to Ques - CodeForces 1181A

Approach
  • It's easy to calculate how much coconuts we will buy: k=x+yz (suppose that all money transferred to a single person, this way the number of bought coconuts would be clearly maximal)
  • If k=xz+yz, then the answer is k,0. The remaining case is a bit harder.
  • Let's notice, that there is no need to transfer z chizhiks, since the one transferring money could have used z chizhiks to buy one more coconut herself.
  • Also it's optimal to transfer coins such that the remainder modulo z of the receiving part will turn to be exactly zero (we could have simply transfer less for the same effect).
  • So the answer is k,min(z(xmodz),z(ymodz)).



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;

public class CF1181A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long x= sc.nextLong();
        long y= sc.nextLong();
        long z= sc.nextLong();
        long total=(x+y)/z;
        if((x/z+y/z)<((x+y)/z))
        {
            long change = Math.min(z-(x%z),z-(y%z));
            System.out.println(total+" "+change);

        }
        else
        {
            System.out.println(total+" 0");
        }
    }
}

Comments

Popular posts from this blog

Class 12 Chemistry Practicals

Class 12 Physics Practicals