BZOJ 1072: [SCOI2007]排列perm

题目

1072: [SCOI2007]排列perm

Time Limit: 10 Sec  Memory Limit: 162 MB

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

题解

C啪啪里有一个算法库,里面有一个next_permutation的函数可以自动生成全排列,然后暴力就好了QAQ

代码

 1 /*Author:WNJXYK*/
 2 #include<cstdio>
 3 #include<algorithm> 
 4 #include<string>
 5 #include<iostream>
 6 using namespace std;
 7 int T;
 8 string s;int d;
 9 long long sum;
10 int sn[15];
11 int sx;
12 int ans=0;
13 bool cmp(int a,int b){
14     if (a<b) return true;
15     return false;
16 }
17 int main(){
18     cin>>T;
19     for (;T--;){
20         cin>>s;cin>>d;
21         ans=0;    
22         for (int i=0;i<s.length();i++){
23             sn[i+1]=s[i]-'0';
24             sx=s.length();
25         }
26         sort(sn+1,sn+sx+1,cmp);
27         do{
28             sum=0;
29             for (int i=1;i<=sx;i++)sum=sum*10+(long long)sn[i];
30             //cout<<sum<<endl;
31             if (sum%(long long)d==0) ans++;
32         }while(next_permutation(sn+1,sn+sx+1));
33         printf("%d
",ans);
34     }
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/WNJXYK/p/4075766.html