Cod sursa(job #1732998)

Utilizator danutbodbodnariuc danut danutbod Data 23 iulie 2016 13:56:23
Problema Suma si numarul divizorilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.01 kb
//100p var IV ciur+descompunere+ formule
//calc.numitorul si numaratorul separat si la sfarsit face inver modular
#include <fstream>
#include <bitset>
using namespace std;
const int MAX = 1000005;
const int MOD = 9973;
ifstream fi ("ssnd.in");
ofstream fo ("ssnd.out");
long long x;
int T, k, P[MAX];
bitset <MAX> viz;

void ciur() {   //ciur direct in vectorul P[] cu k elemente prime
    for(int i = 2; i < MAX; i++)
        if(viz[i] == 0) {
            P[++k] = i;
            for(int j = 2*i; j < MAX; j = j+i) viz[j] = 1;
        }
}
int putere2(long long  n,long long  p)//exponentiere logaritmica recursiva
{
    long long r;
    if (p==0) return 1;
    else
       if (p%2==0){r=putere2(n,p/2)%MOD;return(r*r)%MOD;}
       else  return (putere2(n,p-1)*n)%MOD;
}
//int putere(int n, int p) {  //exponentiere logaritmica
//    int sol = 1;
//    n %= MOD;
//    for(; p; p >>= 1) {
//        if(p & 1)  sol = (sol*n)%MOD;
//        n= (n*n)%MOD;
//    }
//    return sol;
//}

int main() {
    ciur();
    fi >> T;
    for(int j=1; j<=T; j++){
     fi >> x;
     int p1=1,p2=1,nd = 1, sd = 1;
     for(int i = 1; i <= k && 1LL * P[i] * P[i] <= x; i++) //fac desc. cu numere prime
        if(x % P[i]==0){                                   //pana la sqrt(x)
            int p = 0;
            while(x % P[i] == 0) {x/= P[i];p++;}
            nd *= (p+1);
            p1 = (p1*(putere2(P[i], p+1) - 1))%MOD;  //p1 numaratorul il calc. separat
            p2 = p2*(P[i]-1)%MOD;                   //p2 numitorul il calc. separat
            }
            sd = (p1 *putere2(p2,MOD-2)) % MOD;//aici fac (p1/p2)%MOD( invers modular Fermat )
            if(x > 1) {                //daca ramane un factor prim >sqrt(x) 26=2*13(??)e musai la put. 1
                nd *= 2;                  //p^2
                sd = (1LL*sd*(x+1) % MOD);// (p^2-1)/(p-1)=(p+1)
            }
    fo << nd << " " << sd << '\n';
}
return 0;
}
//#include <fstream>// var III ciur+descompunere+ formule 100p
//#include <bitset>
//using namespace std;
//const int MAX = 1000005;
//const int MOD = 9973;
//ifstream fi ("ssnd.in");
//ofstream fo ("ssnd.out");
//long long x;
//int T, k, P[MAX];
//bitset <MAX> viz;
//
//void ciur() {   //ciur direct in vectorul P[] cu k elemente prime
//    for(int i = 2; i < MAX; i++)
//        if(viz[i] == 0) {
//            P[++k] = i;
//            for(int j = 2*i; j < MAX; j = j+i) viz[j] = 1;
//        }
//}
//
//int putere(int n, int p) {  //exponentiere logaritmica
//    int sol = 1;
//    n %= MOD;
//    for(; p; p >>= 1) {
//        if(p & 1)  sol = (sol*n)%MOD;
//        n= (n*n)%MOD;
//    }
//    return sol;
//}
//
//int main() {
//    ciur();
//    fi >> T;
//    for(int j=1; j<=T; j++){
//     fi >> x;
//     int p1,p2,nd = 1, sd = 1;
//     for(int i = 1; i <= k && 1LL * P[i] * P[i] <= x; i++) //fac desc. cu numere prime
//        if(x % P[i]==0){                                   //pana la sqrt(x)
//            int p = 0;
//            while(x % P[i] == 0) {x/= P[i];p++;}
//            nd *= (p+1);
//            p1 = (putere(P[i], p+1) - 1) % MOD;
//            p2 = putere(P[i]-1, MOD-2) % MOD;//pentru invers modular(Fermat )
//            sd = (((sd * p1) % MOD) * p2) % MOD;
//            }
//            if(x > 1) {                //daca ramane un factor prim >sqrt(x) 26=2*13(??)e musai la put. 1
//                nd *= 2;                  //p^2
//                sd = (1LL*sd*(x+1) % MOD);// (p^2-1)/(p-1)=(p+1)
//            }
//    fo << nd << " " << sd << '\n';
//}
//return 0;
//}
////var II 70p divizori pana la sqrt(x)
//#include <fstream>
//const long long   MOD= 9973;
//using namespace std;
//ifstream fi("ssnd.in");
//ofstream fo("ssnd.out");
//long long t,x,d,nrd,sd,i;
//int main()
//{
//    fi>>t;
//    for(i=1;i<=t;i++)
//    {
//        fi>>x;
//        nrd=0;sd=0;
//        for(d=1;d*d<=x;d++)
//            if(x%d==0)
//              if(d!=x/d)
//                {nrd+=2;
//                 sd += d%MOD+(x/d)%MOD;
//                 sd %= MOD;
//                }
//              else
//                {nrd+=1;
//                 sd += d%MOD;
//                 sd %= MOD;
//                }
//        fo<<nrd<<" "<<sd<<'\n';
//    }
//    return 0;
//}
////var I bruta 20p
//#include <fstream>
//#define  MOD 9973;
//using namespace std;
//ifstream fi("ssnd.in");
//ofstream fo("ssnd.out");
//long long t,x,d,nrd,sd,i;
//int main()
//{
//    fi>>t;
//    for(i=1;i<=t;i++)
//    {
//        fi>>x;
//        nrd=0;sd=0;
//        for(d=1;d<=x;d++)
//            if(x%d==0)
//                {nrd++;
//                 sd += d;
//                 sd %= MOD;
//                }
//
//        fo<<nrd<<" "<<sd<<'\n';
//    }
//    return 0;
//}


//int P[M+1];
//bool pr[M+1];
////ciur
//    for(i=2;i<=M;i++)
//      if(pr[i]==0){
//        for(j=2*i;j<=M;j=j+i)pr[j]=1;
//      }
//    for(i=2;i<=M;i++)
//        if(pr[i]==0)P[++k]=i;
//    //for(i=1;i<=k;i++)fo<<P[i]<<" ";