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 ...
随机推荐
- 《前端之路》之二:数据类型转换 && 隐式转换 || 显式转换
目录 02:数据类型转换 && 隐式转换 || 显式转换 02:数据类型转换 && 隐式转换 || 显式转换 在上一个章节中,我们介绍了 JavaScript 的基本的 ...
- 通过Jenkins定期清除为None的镜像
在代码持续交付过程中,依靠Jenkins生产Docker镜像时,会生成许多的名为None的中间镜像,这些镜像在整个项目生产过程完毕后意义不大,还占着空间,需要定期清理,通过手动方式实在是繁琐,也就有了 ...
- LindDotNetCore~添加路由前缀
回到目录 路由前缀就是我们所说的api/values里的api,这里的api可以用其它具体含义的字符表示,如Shop,Order,Game,它可以表示一个个模块,这一般在单体架构里;也可以是一个个小服 ...
- python中的编码与解码
编码与解码 首先,明确一点,计算机中存储的信息都是二进制的 编码/解码本质上是一种映射(对应关系),比如‘a’用ascii编码则是65,计算机中存储的就是00110101,但是显示的时候不能显 ...
- 使用+Leapms查看线性规划的单纯形表,itsme命令
知识点 +Leapms的itsme命令 +Leapms的直接代数模型 查看线性规划直接代数模型的单纯形表和计算过程 +Leapms的直接代数模型 +Leapms的直接代数模型十分简单,只是使用了s.r ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- 一次node-sass安装记录
node-sass的版本是3.9.3 Please restart this script from an administrative PowerShell! 在当前powershell中执行下命令 ...
- 【转载】SQL语句中Union和Union All的区别
在使用到SQL语句进行数据库查询的过程中,如果需要求两个数据集合的并集,一般会使用到联合查询关键字Union或者Union All,其实Union和Union All两者的使用有一定差别,查出来的数据 ...
- java爬虫系列目录
1. java爬虫系列第一讲-爬虫入门(爬取动作片列表) 2. java爬虫系列第二讲-爬取最新动作电影<海王>迅雷下载地址 3. java爬虫系列第三讲-获取页面中绝对路径的各种方法 4 ...
- Tkinter小技巧:如何为窗口右上角的‘x’添加一个自定义的响应函数
不废话,直接上代码 import tkinter as tk from tkinter import messagebox main_window = tk.Tk() main_window.geom ...