// Tadeusz Kielak
#include <iostream>
using namespace std;
#define MAX_CYFR 19
int8_t liczba[ MAX_CYFR ];
int8_t l_cyfr;
int8_t cyfry[ 10 ];
int8_t modula[ 10 ];
bool potyczkowa()
{
if( cyfry[ 0 ] )
return false;
for( int8_t x = 9; x > 1; x --)
{
if( cyfry[ x ] && modula[ x ])
return false;
}
return true;
}
void liczba_na_cyfry( int64_t x)
{
for( int8_t i = 0; i < 10; i ++)
{
cyfry[ i ] = 0;
if( i )
modula[ i ] = x % i;
else
modula[ 0 ] = 1;
}
l_cyfr = 0;
int8_t cyfra;
while( x > 0)
{
cyfra = x % 10;
cyfry[ cyfra ] ++;
liczba[ l_cyfr ++ ] = cyfra;
x /= 10;
}
}
void dodaj_jeden()
{
int8_t pozycja = 0;
int8_t x;
while( pozycja < l_cyfr )
{
liczba[ pozycja ] ++;
if(liczba[ pozycja ] < 10 )
{
x = liczba[ pozycja ];
cyfry[ x ] ++;
cyfry[ x - 1 ] --;
break;
}
else
{
liczba[ pozycja ++ ] = 0;
cyfry[ 9 ] --;
cyfry[ 0 ] ++;
}
}
if( pozycja == l_cyfr )
{
liczba[ pozycja ] = 1;
l_cyfr ++;
cyfry[ 1 ] ++;
}
for( x = 2; x < 10; x ++ )
if( ++ modula[ x ] >= x )
modula[ x ] = 0;
}
int main()
{
int64_t l, r, wynik;
wynik = 0;
cin >> l >> r;
liczba_na_cyfry( l );
for( int64_t x=l; x <= r; x ++)
{
if( potyczkowa())
wynik ++;
dodaj_jeden();
}
cout << wynik << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | // Tadeusz Kielak #include <iostream> using namespace std; #define MAX_CYFR 19 int8_t liczba[ MAX_CYFR ]; int8_t l_cyfr; int8_t cyfry[ 10 ]; int8_t modula[ 10 ]; bool potyczkowa() { if( cyfry[ 0 ] ) return false; for( int8_t x = 9; x > 1; x --) { if( cyfry[ x ] && modula[ x ]) return false; } return true; } void liczba_na_cyfry( int64_t x) { for( int8_t i = 0; i < 10; i ++) { cyfry[ i ] = 0; if( i ) modula[ i ] = x % i; else modula[ 0 ] = 1; } l_cyfr = 0; int8_t cyfra; while( x > 0) { cyfra = x % 10; cyfry[ cyfra ] ++; liczba[ l_cyfr ++ ] = cyfra; x /= 10; } } void dodaj_jeden() { int8_t pozycja = 0; int8_t x; while( pozycja < l_cyfr ) { liczba[ pozycja ] ++; if(liczba[ pozycja ] < 10 ) { x = liczba[ pozycja ]; cyfry[ x ] ++; cyfry[ x - 1 ] --; break; } else { liczba[ pozycja ++ ] = 0; cyfry[ 9 ] --; cyfry[ 0 ] ++; } } if( pozycja == l_cyfr ) { liczba[ pozycja ] = 1; l_cyfr ++; cyfry[ 1 ] ++; } for( x = 2; x < 10; x ++ ) if( ++ modula[ x ] >= x ) modula[ x ] = 0; } int main() { int64_t l, r, wynik; wynik = 0; cin >> l >> r; liczba_na_cyfry( l ); for( int64_t x=l; x <= r; x ++) { if( potyczkowa()) wynik ++; dodaj_jeden(); } cout << wynik << "\n"; return 0; } |
English