hdu 1755 A Number Puzzle

这题枚举k节省时间

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<stdlib.h>
#define ll __int64
#define pi acos(-1.0)
using namespace std;
int
an[9],n,m,x,k,dp[102][102];
bool
vis[9];
void
dfs(int d,int num)
{

    int
i,temp;
    if
(d==n)
    {

        for
(i=1;i<100;i++)
            if
(dp[i][temp=num%i]>num)
                dp[i][temp]=num;
        return
;
    }

    for
(i=0;i<n;i++)
    {

        if
(vis[i]==0)
        {

            if
(d==0&&an[i]==0) continue;
            vis[i]=1;
            dfs(d+1,num*10+an[i]);
            vis[i]=0;
        }
    }
}

int
main()
{

    int
t,i,j;
    while
(scanf("%d%d",&n,&m)!=EOF)
    {

        for
(i=0;i<n;i++)
            scanf("%d",&an[i]);
        sort(an,an+n);
        memset(vis,false,sizeof(vis));
        for
(i=0;i<100;i++)
            for
(j=0;j<100;j++)
                dp[i][j]=1111111111;
        dfs(0,0);
        for
(i=0;i<m;i++)
        {

            scanf("%d%d",&x,&k);
            int
temp=(k-x%k)%k;
            if
(dp[k][temp]!=1111111111)
               cout<<dp[k][temp]<<endl;
            else
cout<<"None"<<endl;
        }
    }

    return
0;
}

posted @ 2013-07-24 12:01  _随心所欲_  阅读(130)  评论(0编辑  收藏  举报