【BZOJ1072】排列(搜索)

题面

BZOJ
洛谷

题解

算下复杂度,如果用 next_permutation n e x t _ p e r m u t a t i o n
那就是 10!×10×15 10 ! × 10 × 15 ,复杂度不太对
那好办啊,把 nextpermutation n e x t p e r m u t a t i o n 改成搜索不就完了。。

#include<iostream>
#include<cstring>
using namespace std;
char ch[15];
int a[10],d,ans,n;
void dfs(int x,int r)
{
    if(x==n+1){ans+=r==0;return;}
    for(int i=0;i<=9;++i)
        if(a[i])--a[i],dfs(x+1,(r*10+i)%d),++a[i];
}
int main()
{
    ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--)
    {
        cin>>(ch+1)>>d;ans=0;n=strlen(ch+1);
        for(int i=n;i;--i)++a[ch[i]-48];
        dfs(1,0);cout<<ans<<endl;
        a[0]=a[1]=a[2]=a[3]=a[4]=a[5]=a[6]=a[7]=a[8]=a[9]=0;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值