Cod sursa(job #3296992)

Utilizator mircea_007Mircea Rebengiuc mircea_007 Data 20 mai 2025 00:37:38
Problema 12-Perm Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#include <assert.h>

#include <vector>
#include <algorithm>
#include <numeric>

using uint = unsigned;
constexpr uint MASK = 1048576 - 1;

int main() {
  FILE *fin = fopen( "12perm.in", "r" );
  FILE *fout = fopen( "12perm.out", "w" );

  int n;
  fscanf( fin, "%d", &n );

  if( n <= 3 ){
    int fact[3 + 1] = { 1, 1, 2, 6 };
    fprintf( fout, "%d\n", fact[n] );
    fclose( fin );
    fclose( fout );
    return 0;
  }

  std::vector<uint> tail({ 1, 1, 1, 2 });
  for( int i = (int)tail.size(); i <= n; i++ )
    tail.push_back( tail[i - 1] + tail[i - 3] + 1 );

  uint ret = (n % 2) * 2 + 2 * tail[n];
  for( int poz_max = 1; 2 * poz_max + 1 < n; poz_max++ ){
    uint cnt_poz_max = 0;
    cnt_poz_max += tail[n - 2 * poz_max];
    cnt_poz_max += tail[n - 2 * poz_max - 1];
    ret += 2 * cnt_poz_max;
  }

  fprintf( fout, "%d\n", int(ret & MASK) );

  fclose( fin );
  fclose( fout );
  return 0;
}