Problem 2. number题解
number:数学+二分图匹配
首先,如果S<N,那么S+1,S+2...N这些数直接放在S+1,S+2...N的位置上(如果其他数x放在这些位置上面,这些数不放在对应位置,那么x一定能放在这些数放的位置,所以直接交换即可)所以可以直接将S和N调换,缩小N。接着看N个连续的数,如果这里面有两个素数,则肯定无解,而在1e9的范围内,素数间隔最大是低于600的,我们就可以通过二分图匹配(s+i与其因数建边)求出最大匹配,若最大匹配为N,则为Yes。实际上,能满足的N其实最大为30多,而菜菜的jyb只枚举到了很多20多的答案,所以为了卡掉暴力匹配的做法(但还是很良心地给了5个点),不得不多设置了很多数据组数。
迷之数学规律,n>600时就会至少有两个素数出现,不符合题意,然后我们一下子就将1e9的数据变成了n<600,之后进行裸的二分图匹配即可,代码如下
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int N=;
bool vis[maxn];
int match[maxn];
int ditu[N][N];
int T,n,s;
bool dfs(int x)//匈牙利算法
{
if(x==)
{
return false;
}
for(int i=;i<=n;i++)
{
if(!vis[i] && ditu[i][x])
{
vis[i]=true;
if(!match[i]||dfs(match[i]))
{
match[i]=x;
return true;
}
}
}
return false;
}
int main()
{
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&s);
if(s<n)
{
swap(s,n);
}
if(n>)
{
printf("No\n");
continue;
}
memset(ditu,,sizeof(ditu));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if((i+s)%j==)//能够整除的数之间建边,这段代码的灵魂所在
{
ditu[i][j]=;
}
}
}
memset(match,,sizeof(match));
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))
{
ans++;
}
}
if(ans==n)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return ;
}
Problem 2. number题解的更多相关文章
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- FZU Problem 1853 Number Deletion
Problem 1853 Number Deletion Accept: 80 Submit: 239 Time Limit: 1000 mSec Memory Limit : 32768 ...
- [LeetCode&Python] Problem 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode&Python] Problem 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- [LeetCode&Python] Problem 806. Number of Lines To Write String
We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
- POJ2104:K-th Number——题解
http://poj.org/problem?id=2104 题目大意:求区间第k小. —————————————————————————— 主席树板子题. ……我看了半天现在还是一知半解的状态所以应 ...
- POJ3468:A Simple Problem with Integers——题解
http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...
- Project Euler:Problem 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
随机推荐
- 如何快速高效简洁的打开软件 干净利索的windows快捷程序启动器
本文的主题是如何高效快捷的打开你想要打开的软件 本文介绍的应该是最简洁的一种方式,借助于windows内部的path进行设置 也可以认为是一种形式的windows应用启动器程序---win+R快速打开 ...
- celery异步消息处理框架
Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...
- (转载)IQueryable和IEnumerable
第一篇:https://www.cnblogs.com/zgqys1980/p/4047315.html: 第二篇:https://www.cnblogs.com/shenbing/p/5394228 ...
- Spring Boot 和 Docker 实现微服务部署
Spring boot 开发轻巧的微服务提供了便利,Docker 的发展又极大的方便了微服务的部署.这篇文章介绍一下如果借助 maven 来快速的生成微服务的镜像以及快速启动服务. 其实将 Sprin ...
- Centos7下用FastDFS搭建图片服务器
1.所用到的工具: 1.FastDFS_v5.05.tar 2.fastdfs-nginx-module_v1.16.tar 3.libfastcommonV1.0.7.tar 4.nginx-1.1 ...
- 001. Java内存中的字符编码
Java内存中的字符编码 Unicode字符集及utf-8 .utf-16.utf-32 等字符编码方式 字符集:字符表示的数字集合,元素称为码点或码位: 字符编码:字符实际的储存表示: 码点:一个码 ...
- 三星5.0以上机器最简单激活Xposed框架的经验
对于喜欢钻研手机的哥们来说,经常会接触到XPOSED框架及种类繁多功能强大的模块,对于5.0以下的系统版本,只要手机能获得ROOT权限,安装和激活XPOSED框架是比较轻易的,但随着系统版本的更新,5 ...
- 如何简单的构建Android?
原文链接:https://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/ 过去的几个月中,在Tuenti上与同行例 ...
- 解决laravel Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found 错误
这个错误的原因来自于没有安装一个依赖库: 官方文档说明如下: Modifying Columns Prerequisites Before modifying a column, be sure to ...
- 码云代码托管平台与TortoiseSVN的使用
1.到https://gitee.com/进行注册,然后登陆 可以发现可以将项目设为私有 2.下载tortoisesvn,一路next安装即可 3.项目创建 4.下载项目 5.创建文件并提交 6.如何 ...