1072: [SCOI2007]排列perm
2017-06-30 20:03
141 查看
Description
给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0)。例如123434有90种排列能
被2整除,其中末位为2的有30种,末位为4的有60种。
Input
输入第一行是一个整数T,表示测试数据的个数,以下每行一组s和d,中间用空格隔开。s保证只包含数字0, 1
, 2, 3, 4, 5, 6, 7, 8, 9.
Output
每个数据仅一行,表示能被d整除的排列的个数。
Sample Input
7
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29
Sample Output
1
3
3628800
90
3
6
1398
利用STL的next_permutation()函数实现全排列暴力
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<set>
#include<map>
using namespace std;
set<int>s;
const int maxm = 10005;
int ans = 0, d, len;
char str[25], ch[25];
int a[25];
int main()
{
int n, i, j, k, sum, t;
scanf("%d", &t);
while (t--)
{
scanf("%s%d", str, &d);
len = strlen(str);
for (i = 0;i < len;i++)
a[i] = str[i] - '0';
sort(a, a + len);
s.clear();
ans = 0;
while (1)
{
long long x = 0;
for (i = 0;i < len;i++)
x = x * 10 + a[i];
if (s.count(x) == 0 && x % d == 0)
{
ans++;
s.insert(x);
}
if (!next_permutation(a, a + len)) break;
}
printf("%d\n", ans);
}
return 0;
}
给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0)。例如123434有90种排列能
被2整除,其中末位为2的有30种,末位为4的有60种。
Input
输入第一行是一个整数T,表示测试数据的个数,以下每行一组s和d,中间用空格隔开。s保证只包含数字0, 1
, 2, 3, 4, 5, 6, 7, 8, 9.
Output
每个数据仅一行,表示能被d整除的排列的个数。
Sample Input
7
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29
Sample Output
1
3
3628800
90
3
6
1398
利用STL的next_permutation()函数实现全排列暴力
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<set>
#include<map>
using namespace std;
set<int>s;
const int maxm = 10005;
int ans = 0, d, len;
char str[25], ch[25];
int a[25];
int main()
{
int n, i, j, k, sum, t;
scanf("%d", &t);
while (t--)
{
scanf("%s%d", str, &d);
len = strlen(str);
for (i = 0;i < len;i++)
a[i] = str[i] - '0';
sort(a, a + len);
s.clear();
ans = 0;
while (1)
{
long long x = 0;
for (i = 0;i < len;i++)
x = x * 10 + a[i];
if (s.count(x) == 0 && x % d == 0)
{
ans++;
s.insert(x);
}
if (!next_permutation(a, a + len)) break;
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 【SCOI2007】【BZOJ1072】排列perm
- BZOJ 1072: [SCOI2007]排列perm
- bzoj1072: [SCOI2007]排列perm
- BZOJ 1072 [SCOI2007] 排列perm
- [BZOJ1072]-[SCOI2007]排列perm-状压DP
- [BZOJ1072][SCOI2007]排列perm(状压dp)
- bzoj1072[SCOI2007]排列perm
- bzoj 1072: [SCOI2007]排列perm
- [BZOJ 1072] [SCOI2007] 排列perm 【状压DP】
- BZOJ1072: [SCOI2007]排列perm 状压DP
- bzoj1072 [SCOI2007]排列perm
- Bzoj1072 [SCOI2007]排列perm
- bzoj1072: [SCOI2007]排列perm
- [BZOJ 1072][SCOI 2007]排列perm
- bzoj 1072: [SCOI2007]排列perm(状压DP)
- BZOJ 1072 [SCOI2007]排列perm
- 1072: [SCOI2007]排列perm
- 1072: [SCOI2007]排列perm
- BZOJ 1072 SCOI2007 排列perm 状压DP
- BZOJ 1072 [SCOI 2007] 排列perm (状压DP)