Kingdom of Obsession
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 305 Accepted Submission(s): 91
Problem Description
There is a kindom of obsession, so people in this kingdom do things very strictly.
They name themselves in integer, and there are n people with their id continuous (s+1,s+2,⋯,s+n) standing in a line in arbitrary order, be more obsessively, people with id x wants to stand at yth position which satisfy
Is there any way to satisfy everyone's requirement?
They name themselves in integer, and there are n people with their id continuous (s+1,s+2,⋯,s+n) standing in a line in arbitrary order, be more obsessively, people with id x wants to stand at yth position which satisfy
xmody=0
Is there any way to satisfy everyone's requirement?
Input
First line contains an integer
T
, which indicates the number of test cases.
Every test case contains one line with two integers n , s .
Limits
1≤T≤100 .
1≤n≤109 .
0≤s≤109 .
Every test case contains one line with two integers n , s .
Limits
1≤T≤100 .
1≤n≤109 .
0≤s≤109 .
Output
For every test case, you should output
'Case #x: y', where
x indicates the case number and counts from
1 and
y is the result string.
If there is any way to satisfy everyone's requirement, y equals 'Yes', otherwise y equals 'No'.
If there is any way to satisfy everyone's requirement, y equals 'Yes', otherwise y equals 'No'.
Sample Input
2 5 14 4 11
Sample Output
Case #1: No Case #2: Yes
Source
对于这个问题,可以发现只有两种情况,如下所示。假设有5个人,那么座位号就是1,2,3,4,5;对于人的id,第一种情况是3,4,5,6,7,即座位号与id号有交集,第二种情况是6,7,8,9,10,即座位号与id号没有交集。对于第一种情况,最优的解肯定是让交集的配对,其他一一匹配。第二种情况就是逐个匹配。可以发现,要匹配的id号中,如果有两个是素数,那么这中情况是不可能的,所以对于要匹配的序列中,直接暴力匹配即可,因为这个队列不会太长(要满足最多只有一个素数)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
using namespace std;
int start;
int n,s;
map<int,int>vis;
bool check(int k)
{
int nn=sqrt(k*1.0);
nn++;
int flag=0;
for(int i=2;i<=nn;i++)
{
if(k%i==0)
{
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}
bool dfs(int k)
{
if(k==1)
{
return true;
}
for(int i=start;i<=s+n;i++)
{
if(i%k==0&&vis[i]==0)
{
vis[i]=1;
if(dfs(k-1))
return true;
vis[i]=0;
}
}
return false;
}
int main()
{
int cas;
int c=0;
scanf("%d",&cas);
while(cas--)
{
vis.clear();
scanf("%d%d",&n,&s);
if(s==0)
{
printf("Case #%d: Yes\n",++c);
continue;
}
start=max(s+1,n+1);
int num=0;
int fl=1;
for(int i=start;i<=s+n;i++)
{
if(check(i))
{
num++;
}
if(num>=2)
{
fl=0;
break;
}
}
if(fl==0)
{
printf("Case #%d: No\n",++c);
continue;
}
if(dfs(min(n,s)))
{
printf("Case #%d: Yes\n",++c);
}
else
{
printf("Case #%d: No\n",++c);
}
}
//system("pause");
}