题目链接:Codeforces 401D Roman and Numbers
题目大意:给出一个最长为18位的数n,以及小于100的数m,现在将n的各个位数重新排列,问说可以组成多少柯整除m的数,不可以有前导0.
解题思路:dp[s][j],表示说集合s下,前面剩余j的情况,有多少种可能。然后对于每种状态开一个大小为10的数组,记录下已经考虑过的数字。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 1<<18;
const int M = 105;
ll n, dp[N+5][M];
int m, c, t[N], v[N+5][M];
void init () {
cin >> n >> m;
c = 0;
while (n) {
t[c++] = n%10;
n /= 10;
}
}
ll solve (int s, int k) {
if (s == 0 && k == 0) return 1;
if (v[s][k]) return dp[s][k];
v[s][k] = 1;
int num[10];
memset(num, 0, sizeof(num));
ll& ans = dp[s][k];
ans = 0;
for (int i = 0; i < c; i++) if (s&(1<<i)) {
if (num[t[i]]) continue;
num[t[i]] = 1;
ans += solve (s^(1<<i), (k*10+t[i])%m);
}
return ans;
}
int main () {
init ();
ll ans = 0;
int s = (1<<c) - 1, num[10];
memset(num, 0, sizeof(num));
for (int i = 0; i < c; i++) if (t[i]) {
if (num[t[i]]) continue;
num[t[i]] = 1;
ans += solve(s^(1<<i), t[i]%m);
}
cout << ans << endl;
return 0;
}
分享到:
相关推荐
Codeforces 1925D Good Trip 题解
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
Educational Codeforces Round 157D. XOR Construction
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Codeforces 1105B - Zuhair and Strings 测试点37个(全)
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
【并查集】Codeforces 566D Restructuring Company题面在这里对于本题,只需要再维护一个并查集表示i所在联通块的最右位置因为相邻
Codeforces 149 D-Coloring Brackets,动态规划求解
Codeforces global round 10 codes
Codeforces round 678 division 2 codes
Some of the Codeforces problems codes
题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,求树的根。 ...操作就是一个删除叶子节点的过程。 AC代码: const int N = 1010;...
题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就是说每个点我们至多遍历一遍,读完题后没什么...
Codeforces round 678 D2_Codeforces_源码
打codeforces的神器