#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int quez_count(int n)
{
int i = 10;
int res = 0;
while(i < n){
res += (i - i/10)*log10(i);
i *= 10;
}
res += (n - i/10)*log10(i)+ceil(log10(n));
return res;
}
int koala_count(int n)
{
int log = log10( n ) + 1;
return ( n + 1 ) * log - ( pow( 10, log) - 1 ) / ( 10 - 1 );
}
void test()
{
srand( time( NULL ) );
cout << "Testing..." << endl;
for( int i = 0; i< 10; ++i)
{
int r = rand() % 100000000 + 100000000;
cout << r << " : " << quez_count( r ) << " : " << koala_count( r ) << endl;
}
}
int main() {
int repeats = 100000,
size = 9500000000;
test();
clock_t start = clock();
for( int i = 0; i< repeats; ++i)
{
int r = rand() % size + size;
quez_count( r );
}
cout << "Time on quez's solution:" <<(double(clock()-start)/CLOCKS_PER_SEC) << "seconds"<<endl;
start = clock();
for( int i = 0; i< repeats; ++i)
{
int r = rand() % size + size;
koala_count( r );
}
cout << "Time on koala's solution:" <<(double(clock()-start)/CLOCKS_PER_SEC) << "seconds"<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGNtYXRoPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHF1ZXpfY291bnQoaW50IG4pCnsKCWludCBpID0gMTA7CglpbnQgcmVzID0gMDsgCgl3aGlsZShpIDwgbil7CgkJcmVzICs9IChpIC0gaS8xMCkqbG9nMTAoaSk7CgkJaSAqPSAxMDsKCX0KCXJlcyArPSAobiAtIGkvMTApKmxvZzEwKGkpK2NlaWwobG9nMTAobikpOwoJcmV0dXJuIHJlczsJCn0KCmludCBrb2FsYV9jb3VudChpbnQgbikKewoJaW50IGxvZyA9IGxvZzEwKCBuICkgKyAxOwoJcmV0dXJuICggbiArIDEgKSAqIGxvZyAgLSAoIHBvdyggMTAsICBsb2cpIC0gMSApIC8gKCAxMCAtIDEgKTsKfQoKdm9pZCB0ZXN0KCkKewoJc3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