Problem 2. number题解

时间:2021-12-03 02:39:18

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题解的更多相关文章

  1. C&num;版 - Leetcode 414&period; Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. FZU Problem 1853 Number Deletion

    Problem 1853 Number Deletion Accept: 80    Submit: 239 Time Limit: 1000 mSec    Memory Limit : 32768 ...

  3. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 447&period; Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  4. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 476&period; Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  5. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 806&period; 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 ...

  6. &lbrack;LeetCode&rsqb;Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

  7. POJ2104:K-th Number——题解

    http://poj.org/problem?id=2104 题目大意:求区间第k小. —————————————————————————— 主席树板子题. ……我看了半天现在还是一知半解的状态所以应 ...

  8. POJ3468:A Simple Problem with Integers——题解

    http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...

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

随机推荐

  1. sqlserver 中数据导入到mysql中的方法以及注意事项

    数据导入从sql server 到mysql (将数据以文本格式从sqlserver中导出,注意编码格式,再将文本文件导入mysql中): 1.若从slqserver中导出的表中不包含中文采用: bc ...

  2. android camera setMeteringArea详解

    摘要: 本文为作者原创,未经允许不得转载:原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/5802814.html setMeteringArea() ...

  3. 电赛总结(二)&mdash&semi;&mdash&semi;AD芯片总结之AD7715

    一.特性参数 1.16位无失真AD转换器 2.增益可调,在1,2,32,128可切换. 3.数字地和模拟地分开,可以减少噪声. 4.具有较大的输出电流,有比较好的带载能力. 二.管脚排列 三.引脚功能 ...

  4. alertdialog&period;builder 自定义弹窗

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  5. mysql 升级方法

    Performing an In-place Upgrade This section describes how to perform an in-place upgrade. Review Bef ...

  6. html5的自定义data-*属性和jquery的data&lpar;&rpar;方法的使用示例

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...

  7. android基本知识&lpar;一&rpar;

    今天开始更新一下android的基本知识,下面是敲代码遇到的问题. 1)我们来谈谈android.intent.category.DEFAULT的用途.     在谈这个tag的用途之前,读者要明白什 ...

  8. HTML中为何P标签内不可包含DIV标签? (转)

    起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...

  9. 添加网站QQ客服链接

    http://wpa.qq.com/msgrd?v=3&uin=3475432549&site=qq&menu=yes 将其中的uin值改为客服QQ即可

  10. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 &lpar;旧版本 &vert; 仅作参考&rpar;

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...