hdu 5943 Kingdom of Obsession

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

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
1T100 .
1n109 .
0s109 .
 

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'.
 

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");
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值