URAL1091---Tmutarakan Exams(dp)

1 篇文章 0 订阅

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: zchao1995@gmail.com 
    > 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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值