Topic links
Test instructions: Give you a,b,n, let you find two number x, Y, make N (a*x+b*y) the smallest.
Idea: Divide the size to do and then enumerate the multiples of a
1#include <stdio.h>2#include <string.h>3#include <iostream>4 #defineLL __int645 6 using namespacestd;7 8 intMain ()9 {Ten LL a,b,n; One while(~SCANF ("%i64d%i64d%i64d",&a,&b,&N)) A { - if(A = =1) - { theprintf"%i64d 0\n", n); - Continue ; - } - if(b = =1) + { -printf"0%i64d\n", n); + Continue ; A } at BOOLFlag =false ; - if(A <b) - { - Swap (A, b); -Flag =true ; - } inLL t = min (n/A, b), X; -LL Minn =999999999LL; to for(inti =0; I <= t; i++) + { - if((n-a * i)% B <Minn) the { *Minn = (n-a * i)%b; $x =i;Panax Notoginseng } - } the if(!flag) printf ("%i64d%i64d\n", X, (n-a*x)/b); + Elseprintf"%i64d%i64d\n", (N-A*X)/b,x); A } the return 0 ; +}
View Code
URAL 1430. Crime and Punishment (number theory)