P4163 [SCOI2007]排列
注意要排序;
next_permutation
prev_permutation
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=100; typedef long long ll; int T,n,d; int a[maxn]; char s[maxn]; ll ans; int main() { scanf("%d",&T); while(T--) { ans=0; scanf("%s",s); scanf("%d",&d); int len=strlen(s); for(int i=1;i<=len;i++) a[i]=s[i-1]-'0'; sort(a+1,a+len+1); do { ll sum=0; for(int i=1;i<=len;i++) { sum=sum*10+a[i]; } if(sum%d==0) ans++; }while(next_permutation(a+1,a+len+1)); printf("%lld ",ans); } return 0; }