您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: