Cod sursa(job #826541)

Utilizator vendettaSalajan Razvan vendetta Data 30 noiembrie 2012 20:55:54
Problema Descompuneri Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <cmath>

using namespace std;

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

#define nmax 3005
#define ll long long
#define MOD 7001
int n, K;
ll dp[nmax][nmax];
vector<int> v;
vector< pair<int,int> > lista[MOD+4];
set<int> S;

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;
        }
    }

}

void rezolva(){

    //aflu divizorii lui n si apoi vreau sa vad in cate moduri il pot obtine pe n
    // fac un dp[i][j] = nr de moduri de a obtine al i-lea divizor folosindu-ma de divizorii 1,..j;
    int sqrtn = sqrt(n);
    for(int i=1; i<=sqrtn; ++i){
        if (n % i != 0) continue;
        S.insert( i );
        S.insert( n/i );
    }
    v.push_back(-1);
    for(set<int>::iterator it=S.begin(); it!=S.end(); it++){
        v.push_back(*it);
    }
    for(int i=0; i<v.size(); ++i){
        baga( v[i], i );
    }
    n = v.size()-1;
    for(int i=1; i<=n; ++i) dp[1][i] = 1;
    for(int i=2; i<=n; ++i){
        //cout << i << ": ";
        for(int j=1; j<=n; ++j){
            dp[i][j] = dp[i][j-1];
            if ( v[i] % v[j] != 0 ) continue;
            int X = v[i] / v[j];
            int poz = afla( X );
            //cout << poz << " " << v[i] << " " << v[j] << " " << j << "\n";
            //poz++;
            dp[i][j] += dp[poz][j];
            //cout << dp[i][j] << " ";
        }
        //cout << '\n';
    }

    g << dp[n][n];

}

int main(){

    citeste();
    rezolva();

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

    return 0;

}