排列I
时间限制: 1 Sec 内存限制: 128 MB
题目描述
给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0)。例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种。
输入
输入第一行是一个整数T,表示测试数据的个数,以下每行一组s和d,中间用空格隔开。s保证只包含数字0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
输出
每个数据仅一行,表示能被d整除的排列的个数。
样例输入
7
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29
样例输出
1
3
3628800
90
3
6
1398
提示
在前三个例子中,排列分别有1, 3, 3628800种,它们都是1的倍数。
100%的数据满足:s的长度不超过10, 1<=d<=1000, 1<=T<=15
题解
next_permutation() 大法好~~~~ `
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
char a[20];
int n,d,ans;
long long x;
scanf("%d",&n);
while(n--)
{
scanf("%s %d",a,&d);
ans=0;
sort(a,a+strlen(a));
if(atoll(a)%d==0)
ans++;
while(next_permutation(a,a+strlen(a)))
{
if(atoll(a)%d==0)
ans++;
}
printf("%d\n",ans);
}
return 0;
}