只看编程题

第一题题意:给一个n,问离最n最近的素数,相同距离优先考虑小的(1≤n≤1e9)

不贴代码了,大家应该都会。

思路:暴力check前后即可,或者用其他的筛法也可以(像欧拉筛)

第二题题意:给一个由1到9组成的数字串,长度不超过15,问排列组合中有多少能被m整除?(1≤m≤50)

自己写的状压,对拍了一下应该没问题(有问题请指正)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long ll;
ll dp[1 << 16][55];
char s[50];

int main() {
    ll l, r, n, m, i, j, t;
    while (scanf("%s %lld", s, &m) != EOF) {
        n = strlen(s);
        memset(dp, 0, sizeof(dp));
        sort(s, s + n);//对数字进行排序
        dp[0][0] = 1;
        for (i = 0; i < (1LL << n); i++) {
            for (j = 0; j < n; j++) {
                if (!(i & (1LL << j))) {
                    if (j == 0 || s[j] != s[j - 1] || (i & (1LL << (j - 1)))) {//保证对于同一数字按序
                        for (t = 0; t < m; t++) {
                            dp[i ^ (1LL << j)][(t * 10 + (s[j] - '0')) % m] += dp[i][t];
                        }
                    }
                }
            }
        }
        printf("%lld\n", dp[(1 << n) - 1][0]);
    }
}

 

posted on 2020-04-02 20:50  Carits  阅读(336)  评论(0编辑  收藏  举报