Cod sursa(job #824980)

Utilizator vendettaSalajan Razvan vendetta Data 27 noiembrie 2012 11:31:59
Problema Descompuneri Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define nmax 1005
#define sqrtN 320005
#define MOD 7001

ifstream f("desc.in");
ofstream g("desc.out");

int n, K, dp[nmax][nmax];
vector<int> v;
vector< pair<int,int> > lista[MOD+2];
void citeste(){

    f >> n >> K;

}

void baga(int X, int poz){

    int rest = X % MOD;
    lista[rest].push_back( make_pair(X, poz) );

}

int afla(int X){

    int rest = X % MOD;
    for(int i=0; i<lista[rest].size(); ++i){
        if (lista[rest][i].first == X){
            return (lista[rest][i].second);
        }
    }

    return -1;

}

void rezolva(){

    int sqrtx = sqrt(n);
    //cout << sqrtx << "  "<< n << "\n";
    for(int i=1; i<sqrtx+1; ++i){
        if (n % i != 0) continue;
        //cout << i << "\n";
        int X = n / i;
        if (X != i){
            v.push_back(i);
            v.push_back(X);
        }else v.push_back(i);
    }

    sort(v.begin(), v.end() );

    for(int i=0; i<v.size(); ++i){
        baga(v[i], i);
    }

    //dp[i][j] numarul de moduri de a obtine al j- lea divizor daca ma folosesc si de al i-lea divizor

    dp[0][0] = 1;

    for(int i=1; i<v.size(); ++i){
        for(int j=0; j<v.size(); ++j) dp[i][j] = dp[i-1][j];//, cout << dp[i][j] << " ";
        for(int j=0; j<v.size(); ++j){
            if ( v[j] % v[i] != 0) continue;
            int poz = afla(v[j]/v[i]);
            //cout << v[i] << " " << v[j] << " " << v[j]/v[i] << " " << poz << " " << dp[i][poz]<< " " << dp[i][j]<< "\n";
            if (poz == -1) continue;
            dp[i][j] += dp[i][ poz ];
            //cout << dp[i][j] << " ";
        }
        //cout << "\n";
    }

    //cout << dp[v.size()-1][v.size()-1] << "\n";
    g << dp[v.size()-1][v.size()-1] << "\n";

}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}