2023-01-04 17:14:28 -03:00
# ifndef STDIO_H
# define STDIO_H
# include <stdio.h>
# endif
# ifndef STRING_H
# define STRING_H
# include <string.h>
# endif
# ifndef STDLIB_H
# define STDLIB_H
# include <stdlib.h>
# endif
# ifndef SKOMBLIB_H
# define SKOMBLIB_H
typedef struct pair {
int x ;
int y ;
} pair ;
typedef struct trio {
int x ;
int y ;
int z ;
} trio ;
typedef struct quartet {
int x ;
int y ;
int z ;
int w ;
} quartet ;
typedef struct quintet {
int x ;
int y ;
int z ;
int w ;
int v ;
} quintet ;
2023-01-06 00:52:30 -03:00
void readline ( char * line , int len ) ; /* read len ammount of characters from stdin into line */
int splitc_toi ( char * line , int sep , int * dest ) ; /* split a char array of numbers into an int array, separator used between numbes must be provided in the function call (e.g. commas, spaces, etc.) */
int skomb2 ( int number , int * not_allowed , int * not_allowed_len , pair * pairs ) ; /* stores valid combinations that sum up to number respecting an int array of not allowed numbers with it's length in a pair array */
int skomb3 ( int number , int * not_allowed , int * not_allowed_len , trio * trios ) ; /* same as above but with trios instead */
int skomb4 ( int number , int * not_allowed , int * not_allowed_len , quartet * quartets ) ; /* same as above but with quartets instead */
int skomb5 ( int number , int * not_allowed , int * not_allowed_len , quintet * quintets ) ; /* same as above but with quintets instead */
2023-01-04 17:14:28 -03:00
void readline ( char * line , int len ) {
int i = 0 ;
int c ;
while ( ( c = getchar ( ) ) ! = ' \n ' & & i < len ) {
line [ i ] = c ;
i + + ;
}
line [ i ] = ' \0 ' ;
}
int splitc_toi ( char * line , int sep , int * dest ) {
int i , j ;
i = j = 0 ;
char temp [ 2 ] ;
temp [ 1 ] = ' \0 ' ;
while ( line [ i ] ! = ' \0 ' ) {
if ( line [ i ] = = sep ) {
i + + ;
continue ;
}
2023-01-09 14:12:31 -03:00
temp [ 0 ] = line [ i ] ;
dest [ j ] = atoi ( temp ) ;
i + + ;
j + + ;
2023-01-04 17:14:28 -03:00
}
return j ;
}
2023-01-06 00:52:30 -03:00
int skomb2 ( int number , int * not_allowed , int * not_allowed_len , pair * dest ) {
2023-01-04 17:14:28 -03:00
int i , j , k ;
int skip ;
k = 0 ;
for ( i = 1 ; i < 10 ; i + + ) {
for ( j = 0 ; j < * not_allowed_len ; j + + ) {
if ( i = = not_allowed [ j ] | | number - i = = not_allowed [ j ] ) {
skip = 1 ;
break ;
}
}
if ( skip = = 1 ) {
skip = 0 ;
continue ;
}
if ( i ! = number & & i < number - i & & number - i < 10 ) {
dest [ k ] . x = i ;
dest [ k ] . y = number - i ;
k + + ;
}
}
return k ;
}
2023-01-06 00:52:30 -03:00
int skomb3 ( int number , int * not_allowed , int * not_allowed_len , trio * trios ) {
2023-01-04 17:14:28 -03:00
int i , j , k ;
int skip , len ;
k = 0 ;
pair * pairs = malloc ( sizeof ( pair ) * 4 ) ;
for ( i = 1 ; i < 10 ; i + + ) {
for ( j = 0 ; j < * not_allowed_len ; j + + ) {
if ( i = = not_allowed [ j ] ) {
skip = 1 ;
break ;
}
}
if ( skip = = 1 ) {
skip = 0 ;
continue ;
}
if ( i ! = number & & number - i < 18 & & number - i > 2
& & i < number ) {
not_allowed [ * not_allowed_len ] = i ;
( * not_allowed_len ) + + ;
2023-01-06 00:52:30 -03:00
len = skomb2 ( number - i , not_allowed , not_allowed_len , pairs ) ;
2023-01-04 17:14:28 -03:00
if ( len > 0 ) {
for ( j = 0 ; j < len ; j + + ) {
if ( i < pairs [ j ] . x & & i < pairs [ j ] . y ) {
trios [ k ] . x = i ;
trios [ k ] . y = pairs [ j ] . x ;
trios [ k ] . z = pairs [ j ] . y ;
k + + ;
}
}
}
( * not_allowed_len ) - - ;
not_allowed [ * not_allowed_len ] = 0 ;
}
}
free ( pairs ) ;
return k ;
}
2023-01-06 00:52:30 -03:00
int skomb4 ( int number , int * not_allowed , int * not_allowed_len , quartet * quartets ) {
2023-01-04 17:14:28 -03:00
int i , j , k ;
int skip , len ;
k = 0 ;
2023-01-09 19:56:42 -03:00
trio * trios = malloc ( sizeof ( trio ) * 8 ) ;
2023-01-04 17:14:28 -03:00
for ( i = 1 ; i < 10 ; i + + ) {
for ( j = 0 ; j < * not_allowed_len ; j + + ) {
if ( i = = not_allowed [ j ] ) {
skip = 1 ;
break ;
}
}
if ( skip = = 1 ) {
skip = 0 ;
continue ;
}
if ( i ! = number & & number - i < 25 & & number - i > 5 ) {
not_allowed [ * not_allowed_len ] = i ;
( * not_allowed_len ) + + ;
2023-01-06 00:52:30 -03:00
len = skomb3 ( number - i , not_allowed , not_allowed_len , trios ) ;
2023-01-04 17:14:28 -03:00
if ( len > 0 ) {
for ( j = 0 ; j < len ; j + + ) {
if ( i < trios [ j ] . x & & i < trios [ j ] . y & & i < trios [ j ] . z ) {
quartets [ k ] . x = i ;
quartets [ k ] . y = trios [ j ] . x ;
quartets [ k ] . z = trios [ j ] . y ;
quartets [ k ] . w = trios [ j ] . z ;
k + + ;
}
}
}
( * not_allowed_len ) - - ;
not_allowed [ * not_allowed_len ] = 0 ;
}
}
free ( trios ) ;
return k ;
}
2023-01-06 00:52:30 -03:00
int skomb5 ( int number , int * not_allowed , int * not_allowed_len , quintet * quintets ) {
2023-01-04 17:14:28 -03:00
int i , j , k ;
int skip , len ;
k = 0 ;
2023-01-09 19:56:42 -03:00
quartet * quartets = malloc ( sizeof ( quartet ) * 12 ) ;
2023-01-04 17:14:28 -03:00
for ( i = 1 ; i < 10 ; i + + ) {
for ( j = 0 ; j < * not_allowed_len ; j + + ) {
if ( i = = not_allowed [ j ] ) {
skip = 1 ;
break ;
}
}
if ( skip = = 1 ) {
skip = 0 ;
continue ;
}
if ( i ! = number & & number - i < 31 & & number - i > 9 ) {
not_allowed [ * not_allowed_len ] = i ;
( * not_allowed_len ) + + ;
2023-01-06 00:52:30 -03:00
len = skomb4 ( number - i , not_allowed , not_allowed_len , quartets ) ;
2023-01-04 17:14:28 -03:00
if ( len > 0 ) {
for ( j = 0 ; j < len ; j + + ) {
if ( i < quartets [ j ] . x & & i < quartets [ j ] . y & & i < quartets [ j ] . z & & i < quartets [ j ] . w ) {
quintets [ k ] . x = i ;
quintets [ k ] . y = quartets [ j ] . x ;
quintets [ k ] . z = quartets [ j ] . y ;
quintets [ k ] . w = quartets [ j ] . z ;
quintets [ k ] . v = quartets [ j ] . w ;
k + + ;
}
}
}
( * not_allowed_len ) - - ;
not_allowed [ * not_allowed_len ] = 0 ;
}
}
free ( quartets ) ;
return k ;
}
# endif