URAL1091---Tmutarakan Exams(dp)

University of New Tmutarakan trains the first-class specialists in mental arithmetic. To enter the University you should master arithmetic perfectly. One of the entrance exams at the Divisibility Department is the following. Examinees are asked to find K different numbers that have a common divisor greater than 1. All numbers in each set should not exceed a given number S. The numbers K and S are announced at the beginning of the exam. To exclude copying (the Department is the most prestigious in the town!) each set of numbers is credited only once (to the person who submitted it first).
Last year these numbers were K=25 and S=49 and, unfortunately, nobody passed the exam. Moreover, it was proved later by the best minds of the Department that there do not exist sets of numbers with the required properties. To avoid embarrassment this year, the dean asked for your help. You should find the number of sets of K different numbers, each of the numbers not exceeding S, which have a common divisor greater than 1. Of course, the number of such sets equals the maximal possible number of new students of the Department.
Input
The input contains numbers K and S (2 ≤ K ≤ S ≤ 50).
Output
You should output the maximal possible number of the Department’s new students if this number does not exceed 10000 which is the maximal capacity of the Department, otherwise you should output 10000.
Sample
input output

3 10

11

dp[i][j][k]前i个数选了j个数,gcd为k的方案数

/************************************************************************* > File Name: ural1091.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年05月28日 星期四 15时08分59秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL dp[55][55][55];

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
    int K, S;
    while (~scanf("%d%d", &K, &S)) {
        for (int i = 0; i <= S; ++i) {
            for (int j = 0; j <= K; ++j) {
                for (int k = 1; k <= S; ++k) {
                    dp[i][j][k] = 0;
                }
            }
        }
        dp[1][1][1] = 1;
        for (int i = 1; i < S; ++i) {
            dp[i + 1][1][i + 1] = 1;
            for (int j = 1; j <= S; ++j) {
                for (int k = 1; k <= S; ++k) {
                    if (!dp[i][j][k]) {
                        continue;
                    }
                    dp[i + 1][j][k] += dp[i][j][k];
                    int g = gcd(k, i + 1);
                    dp[i + 1][j + 1][g] += dp[i][j][k];
                }
            }
        }
        LL ans = 0;
        for (int i = 2; i <= S; ++i) {
            ans += dp[S][K][i];
        }
        if (ans > 10000) {
            ans = 10000;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

你可能感兴趣的:(dp)