Cod sursa(job #2666701)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 2 noiembrie 2020 13:41:16
Problema Suma divizorilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <stdio.h>
#define NRDIVDIFF 8
#define MOD 9901

int divizori[NRDIVDIFF], exponent[NRDIVDIFF];

void descompunere( int n, int *nrdiv  ) {
    int d = 2, exp, x;
    while ( n >= d * d ) {
        exp = 0;
        while ( n % d == 0 ) {
            exp++;
            n /= d;
        }
        if ( exp > 0 ) {
            divizori[*nrdiv] = d;
            exponent[*nrdiv] = exp;
            x = *nrdiv;
            *nrdiv = x + 1;
        }
        d++;
    }
    if ( n > 1 ) {
        divizori[*nrdiv] = n;
        exponent[*nrdiv] = 1;
        x = *nrdiv;
        *nrdiv = x + 1;
    }
}

int lgput( int a, int b ) {
    int put = 1;
    while ( b > 0 ) {
        if ( b % 2 == 1 )
            put = (long long)put * a % MOD;
        a = (long long)a * a % MOD;
        b /= 2;
    }
    return put;
}

int main()
{
    FILE *fin, *fout;
    int a, b, s, nrdiv, i, put, invs;
    fin = fopen( "sumdiv.in", "r" );
    fout = fopen( "sumdiv.out", "w" );
    fscanf( fin, "%d%d", &a, &b );
    fclose( fin );
    nrdiv = 0;
    descompunere( a, &nrdiv );
    s = 1;
    for ( i = 0; i < nrdiv; i++ ) {
        if ( divizori[i] % MOD == 1 ) {
          s = s * ( exponent[i] * b + 1 ) % MOD;
        }
        else {
          put = lgput( divizori[i], exponent[i] * b + 1 );/// pk^(ek + 1)
          put -= 1;
          if ( put == -1 ) {
              put = 9900;
          }
          invs = divizori[i] - 1;
          invs = lgput( invs, MOD - 2 );
          put = (long long)put * invs % MOD;
          s = ((long long)s * put) % MOD;
        }
    }
    fprintf( fout, "%d", s );
    fclose( fout );
    return 0;
}