暴力,next_permutation函数用于枚举出下一个排列。sscanf函数用于将字符串转化成数字。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,len,ans; 6 long long x,t; 7 char s[15]; 8 int main() 9 { 10 scanf("%d",&n); 11 for(int i=1;i<=n;i++) 12 { 13 ans=0; 14 scanf("%s%lld",s,&x); 15 len=strlen(s); 16 sort(s,s+len); 17 sscanf(s,"%lld",&t); 18 if(!(t%x))ans++; 19 while(next_permutation(s,s+len)) 20 { 21 sscanf(s,"%lld",&t); 22 if(!(t%x))ans++; 23 } 24 printf("%d ",ans); 25 } 26 return 0; 27 }