bzoj1072: [SCOI2007]排列perm

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1411  Solved: 879
[Submit][Status][Discuss]

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

HINT

 

在前三个例子中,排列分别有1, 3, 3628800种,它们都是1的倍数。

【限制】

100%的数据满足:s的长度不超过10, 1<=d<=1000, 1<=T<=15

题解:

  生成排列,放到set里,以便以后检查是否重复

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<set>
 8 using namespace std;
 9 typedef long long LL;
10 int T, d, l, Ans;
11 int A[15];
12 char Str[15];
13 LL Num;
14 set<LL> S;
15 int main(){
16     scanf("%d",&T);
17     for (int Case=1;Case<=T;Case++){
18         scanf("%s%d", Str, &d);
19         Ans=0;
20         S.clear();
21         l=strlen(Str);
22         for(int i=0;i<l;i++) A[i]=Str[i]-'0';
23         sort(A,A+l);
24         while(true){
25             Num=0;
26             for(int j=0;j<l;j++) Num=Num*10+A[j];
27             if(S.count(Num)==0&&Num%d==0){
28                 ++Ans;
29                 S.insert(Num);
30             }
31             if(!next_permutation(A,A+l)) break;
32         }
33         printf("%d
", Ans);
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/CXCXCXC/p/5080386.html