poj专题 - 初期数学

第六个专题了,初期数学:


(1)、组合数学

1、加法原理和乘法原理以及排列组合

 

1、hdu 4497 GCD and LCM

题意:已知l,g其中g=gcd(x,y,z),l=lcm(x,y,z),问x,y,z有多少种组合使得关系成立。

分析:

最大公约数和最小公倍数有以下式子成立:

所以对g进行唯一定理的分解,对任意一个素因子,设其在g和l中的指数分别为a,b,则必有a <= b,且x,y,z中必有一个数指数为a,一个为b,至于另一个数其指数在a和b之间都行,根据排列组合很容易可以推导出计算一个素因子时的组合数为6*(l – g),然后对于所有素因子的情况根据乘法原理便可以求出总共的可能数。

#include <cstdio>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;

typedef long long LL;
map<LL, int> dg, dl;
LL g, l;
void factor(map<LL, int>& mp, int x) {
    for (int i = 2; i * i <= x; i++) {
        while (x % i == 0) mp[i]++, x /= i;
    }
    if (x != 1) mp[x] = 1;
}
bool cal() {
    for (map<LL, int>::iterator i = dg.begin(); i != dg.end(); i++)
        if (i->second > dl[i->first]) return 0;
    return 1;
}
void init() {
    dg.clear(); dl.clear();
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%I64d %I64d", &g, &l);
        init();
        factor(dg, g); factor(dl, l);
        LL ans = cal();
        for (map<LL, int>::iterator i = dl.begin(); ans && i != dl.end(); i++) ans *= i->second == dg[i->first] ? 1 : 6 * (i->second - dg[i->first]);
        printf("%I64d\n", ans);
    }
    return 0;
}

2、hdu5719 Arrange

题意:Ai是1~n的某种排列,Bi表示Ai中前i个数的最小值,Ci表示Ai中前i个数的最大值,给出B和C,求A序列有多少种可能。

分析:分析可知一下得到基本的几点:

1、第一,如果某个位置i最小值和最大值变化了,那么i位置就为变化的值,且不可能同时变化,如果某个位置最小值和最大值都没变化,那么该位置不确定。2、第二,b序列逐渐递减,c序列逐渐递增,对于某个不确定的位置, 对于任何i,必有bi <= bi-1, ci >= ci-1,所以,对于不确定的位置,其数所在的范围也在逐渐增大,也就是说后面位置要确定的数的范围比前面要确定的数的范围大,且对于某个位置i,i以前所有已确定位置的数必定在bi到ci之间,这样的话就是一个明显的乘法原理了,对于某个不确定的位置i,很容易知道其有多少种可能,直接全部乘起来即为答案。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 100010;
const int MOD = 998244353;
int b[N], c[N];

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) scanf("%d", &b[i]);
        for (int i = 0; i < n; i++) scanf("%d", &c[i]);
        LL ans = 1;
        if (b[0] != c[0]) ans = 0;
        for (int i = 1; i < n && ans; i++) {
            if (b[i] <= b[i-1] && c[i] >= c[i-1]) {
                if (b[i] < b[i-1] && c[i] > c[i-1]) ans = 0;
                if (b[i] == b[i-1] && c[i] == c[i-1]) ans *= c[i] - b[i] + 1 - i, ans %= MOD;
            }
            else ans = 0;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

2、递推关系

1、poj 3252 Round Numbers

题意:计算一个区间内二进制位中0的个数比1的多的数的个数。
分析:题目归结为递推关系,我还以为是数位dp,但是强行用排列组合做完了这题。。。
花了整整一个上午才做完。。。
对于给定的两个数,比如说10010、1000100001,其位数分别为5和10.
首先把位数在6和9之间的种数计算出来,然后加上位数为5和10的个数即为答案。
位数在6和9之间的种数很容易知道,关键是5和10:
位数为5的我们需要计算比10010大的数,从10010的第二位开始,如果某位为0,则令其为1,后面的位可以随便取,求符合条件的组合数。
同理,位数为10的我们需要计算比1000100001小的数,从1000100001的第二位开始,如果某位为1,则令其为0,后面的位都可以随便取,求符合条件的组合数。
根据以上的原则所求取的数目并不是答案,因为这样做忽略了一个问题:位数相同时有重复的计算,比如说2到3之间的数计算出来就为2。这样要采取另外的原则:
比如说10010、11001,从正面考虑不容易计算,从反面出发,首先计算出比位数为5且10010小的数目,以及位数为5且比11001大的数目,在计算出位数为5的所有可能的种数,减去前面的两种就是10010到11001之间的个数了。
不过本题完全可以用数位dp实现,在此就不多说了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int N = 32;
int c[N][N];
LL num[N][N];
void deal(LL s, int* s1, int* bs, int& len) {
    while (s) bs[len++] = s & 1, s >>= 1;
    s1[0] = 1;
    for (int i = len - 2, j = 1; i >= 0; i--, j++) s1[j] = s1[j-1] + (bs[i] ? 1 : 0);
};
int bs[N], bf[N], s1[N], f1[N];
int lens, lenf;
int main() {
    for (int i = 0; i < N; i++) num[i][0] = c[i][0] = c[i][i] = 1;
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < i; j++) c[i][j] = c[i-1][j] + c[i-1][j-1];
    }
    for (int i = 1; i < N; i++) {
        for (int j = 1; j <= i; j++) num[i][j] = num[i][j-1] + c[i][j];
    }
    LL s, f;
    scanf("%I64d %I64d", &s, &f);
    deal(s, s1, bs, lens);
    deal(f, f1, bf, lenf);
    LL ans = 0;
    if (lens == lenf) {
        LL tf = 0, ts = 0, tmp;
        LL a = lens >> 1;
        for (int i = lens - 2, j = 0; i >= 0; i--, j++) {
            if (bs[i]) {
                int t = s1[j];
                if (a - t >= 0) ts += a - t >= i ? num[i][i] : num[i][a-t];
            }
        }
        for (int i = lenf - 2, j = 0; i >= 0; i--, j++) {
            if (!bf[i]) {
                int t = f1[j] + 1;
                if (a - t >= 0) tf += a - t >= i ? num[i][i] : num[i][a-t];
            }
        }
        lens--;
        tmp = lens & 1 ? num[lens][lens/2] : num[lens][lens/2-1];
        ans = tmp - ts - tf;
    }
    else {
        LL a = lens >> 1;
        if (s1[lens-1] <= a) ans++;
        for (int i = lens - 2, j = 0; i >= 0; i--, j++) {
            if (!bs[i]) {
                int t = s1[j] + 1;
                if (a - t >= 0) ans += a - t >= i ? num[i][i] : num[i][a-t];
            }
        }
        LL tmp = ans;
        a = lenf >> 1;
        if (f1[lenf-1] <= a) ans++;
        for (int i = lenf - 2, j =
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值