Untitled
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 751 Accepted Submission(s): 403
Problem Description
There is an integer
a
and
n
integers
b1,…,bn
. After selecting some numbers from
b1,…,bn
in any order, say
c1,…,cr
, we want to make sure that
a mod c1 mod c2 mod… mod cr=0
(i.e.,
a
will become the remainder divided by
ci
each time, and at the end, we want
a
to become
0
). Please determine the minimum value of
r
. If the goal cannot be achieved, print
−1
instead.
Input
The first line contains one integer
T≤5
, which represents the number of testcases.
For each testcase, there are two lines:
1. The first line contains two integers n and a ( 1≤n≤20,1≤a≤106 ).
2. The second line contains n integers b1,…,bn ( ∀1≤i≤n,1≤bi≤106 ).
For each testcase, there are two lines:
1. The first line contains two integers n and a ( 1≤n≤20,1≤a≤106 ).
2. The second line contains n integers b1,…,bn ( ∀1≤i≤n,1≤bi≤106 ).
Output
Print
T
answers in
T
lines.
Sample Input
2 2 9 2 7 2 9 6 7
Sample Output
2 -1
Source
题目大意:找到最少的数字 满足这么个条件:a mod c1 mod c2 mod… mod cr=0、其实就是找c的和是a的倍数,这个东西谁看都明白,然后给你n个数,让你去找最少的数字和、这里我直接想的就是搜索去做,但是捏~用什么方法很重要,这里我还是怕深搜超时、没有剪枝的话是很可能超时的,不如用bfs来的实惠、所以第一发写的bfs、实力MLE、、、、、、代码交给大家参考,仅供参考、、反正过不了:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long int
struct date
{
ll val;
int vis[21];
int cont;
}now,nex;
int n;
ll a;
ll b[21];
void bfs()
{
queue<date>s;
for(int i=0;i<n;i++)
{
now.val=b[i];
for(int j=0;j<n;j++)
{
now.vis[j]=0;
}
now.vis[i]=1;
now.cont=1;
s.push(now);
}
while(!s.empty())
{
now=s.front();
s.pop();
if(now.val%a==0)
{
printf("%d\n",now.cont);
return ;
}
for(int i=0;i<n;i++)
{
nex=now;
if(now.vis[i]==0)
{
nex.val=now.val+b[i];
nex.cont=now.cont+1;
nex.vis[i]=1;
s.push(nex);
}
}
}
printf("-1\n");
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d",&n,&a);
for(int i=0;i<n;i++)
{
scanf("%I64d",&b[i]);
}
bfs();
}
}
然后给代码修了修,无果,转战DFS、想到了排序,但是仅仅是先闪烁了一下这个思路,还是无脑dfs了一发,实力TLE、、、真是个令人悲伤的故事、这里也上代码,交给大家参考、、、
a mod c1 mod c2 mod… mod cr=0a mod c1 mod c2 mod… mod cr=0
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long int
int n;
ll a;
ll b[21];
int vis[21];
int output;
void dfs(ll val,int cont)
{
if(val%a==0&&val!=0)
{
output=min(output,cont);
return ;
}
ll x;
for(int i=0;i<n;i++)
{
if(vis[i]==0)
{
x=val+b[i];
vis[i]=1;
dfs(x,cont+1);
vis[i]=0;
}
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d",&n,&a);
for(int i=0;i<n;i++)
{
scanf("%I64d",&b[i]);
}
memset(vis,0,sizeof(vis));
output=0x1f1f1f1f;
dfs(0,0);
if(output!=0x1f1f1f1f)
printf("%d\n",output);
else
{
printf("-1\n");
}
}
}
剪枝无果,最后上排序,30+ms就过了、、、
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int output;
int b[21];
int n,mubiao;
void dfs(int val,int cont,int ans)
{
if(val==0)
{
output=min(cont,ans);
return;
}
if(cont==n)
return ;
dfs(val%b[cont],cont+1,ans+1);
dfs(val,cont+1,ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
output=10000;
scanf("%d%d",&n,&mubiao);
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
sort(b,b+n);
reverse(b,b+n);
dfs(mubiao,0,0);
if(output==10000)
{
printf("-1\n");
}
else
{
printf("%d\n",output);
}
}
}