Advertisement
Guest User

2

a guest
Apr 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. /**
  2.  * Problema: Sipet O( N )
  3.  * Stud. Popescu Silviu Emil
  4.  *    Automatica si Calculatoare
  5.  *    Universitatea Politehnica Bucuresti
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #define NMax 10000010
  11.  
  12. const char IN[] = "sipet.in", OUT[] = "sipet.out";
  13.  
  14. struct pereche {
  15.     int a, b;
  16. };
  17.  
  18. int Tes, N, L, p[4];
  19. int A, B, C, R;
  20. pereche v[NMax];
  21. bool bb[NMax];
  22.  
  23. bool isPrime( int x ) {
  24.  
  25.     for ( int d = 2; d * d <= x; ++ d )
  26.         if ( x % d == 0 )
  27.             return false;
  28.     return true;
  29.  
  30. }
  31.  
  32. int sqr( int x ) {
  33.     return x * x;
  34. }
  35.  
  36. int main() {
  37.  
  38.     freopen(IN, "r", stdin);
  39.     freopen(OUT, "w", stdout);
  40.  
  41.     scanf("%d", &Tes);
  42.  
  43.     while ( Tes -- ) {
  44.  
  45.         scanf("%d%d", &N, &p[1]);
  46.  
  47.         assert(isPrime(p[1]));
  48.         for ( int i = 2; i <= 3; ++ i )
  49.             for ( p[i] = p[i - 1] + 1; !isPrime(p[i]); ++ p[i] );
  50.  
  51.        // fprintf(stderr, "%d %d %d\n", p[1], p[2], p[3]);
  52.         for ( int a = 0; a * p[1] <= N; ++ a )
  53.             for ( int b = 0; a * p[1] + b * p[2] <= N && b < p[1]; ++ b ) {
  54.                 int pos = a * p[1] + b * p[2];
  55.                 v[pos].a = a;
  56.                 v[pos].b = b;
  57.                 bb[pos] = true;
  58.             }
  59.  
  60.         bool found = false; R = -1;
  61.         for ( int r = 0; r <= p[1] && R == -1; ++ r )
  62.             for ( int c = 0; r + c * p[3] <= N && c <= p[1]; ++ c ) {
  63.                 int pos = c * p[3] + r;
  64.                 if ( bb[N - pos] && (R == -1 || R != -1 && A + B + C < v[N - pos].a + v[N - pos].b + c )) {
  65.                     found = true;
  66.                     A = v[N - pos].a;
  67.                     B = v[N - pos].b;
  68.                     C = c;
  69.                     R = r;
  70.                 }
  71.             }
  72.  
  73.         for ( int a = 0; a * p[1] <= N; ++ a )
  74.             for ( int b = 0; a * p[1] + b * p[2] <= N && b < p[1]; ++ b ) {
  75.                 int pos = a * p[1] + b * p[2];
  76.                 bb[pos] = false;
  77.             }
  78.  
  79.         printf("%d %d %d %d %d\n", A + B + C, A, B, C, R);
  80.  
  81.     }
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement