当时想到的第一个想法是用拓展欧几里得解方程,求x的最小正解。一发交了之后发现爆long long,因为m是1e9。

因此本题的正解是暴力,保证有解的情况下,那么a数组中的一个数必然对应着b数组中的一个数,因此,可以遍历数组a,求出b[1]和a[i]对应的x的值,然后再判断是否符合其他元素即可。

要求满足(a+x)%m=b的x的值,可以通过x=((b-a)%m+m)%m,当时就是没有想到这个。

因为求最小值,所有要遍历所有可能。

 #include <bits/stdc++.h>
using namespace std;
const int N=;
int a[N],b[N];
bool check(int x,int m,int n)
{
int c[N]={};
for(int i=;i<=n;i++)
c[i]=(a[i]+x)%m;
sort(c+,c++n);
for(int i=;i<=n;i++)
{
if(b[i]!=c[i])
return false;
}
return true;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int j=;j<=n;j++)
scanf("%d",&b[j]);
sort(b+,b++n);
int ans=0x3f3f3f3f;
for(int i=;i<=n;i++)
{
int x=((b[]-a[i])%m+m)%m;
if(check(x,m,n))
ans=min(x,ans);
}
printf("%d\n",ans);
}
return ;
}

B. Modulo Equality的更多相关文章

  1. Codeforces Round #609 (Div. 2) 题解

    Equation Modulo Equality Long Beautiful Integer Domino for Young K Integers Equation \[ Time Limit: ...

  2. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  3. Codeforces Round #609 (Div. 2) A-E简要题解

    contest链接:https://codeforces.com/contest/1269 A. Equation 题意:输入一个整数,找到一个a,一个b,使得a-b=n,切a,b都是合数 思路:合数 ...

  4. cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. only for equality comparisons Hash Index Characteristics

    http://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html Hash Index Characteristics Hash indexes ...

  6. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. POJ1995 Raising Modulo Numbers

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6373   Accepted: ...

  8. Determining Equality of Objects

    [Determining Equality of Objects] If you need to determine whether one object is the same as another ...

  9. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

随机推荐

  1. CMake中的两种变量(Variable types in CMake)

    在CMake中存在两种变量:normal variables and cache varialbes .正常变量就像是脚本内部变量,相当于程序设计中定义的局部变量那样.而CMakeLists.txt相 ...

  2. CSS selector? [class*=“span”]怎么理解

    我在Twitter 中有看到如下selector: .show-grid [class*="span"] { background-color: #eee; text-align: ...

  3. angularJS 传参的四种方法

    AngularJS - Passing data between pages 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Ye Huang链接:https://www.z ...

  4. Java集合中removeIf的使用

    在JDK1.8中,Collection以及其子类新加入了removeIf方法,作用是按照一定规则过滤集合中的元素.这里给读者展示removeIf的用法.首先设想一个场景,你是公司某个岗位的HR,收到了 ...

  5. redis说明及部署

    一.reids 概述 redis全称REmote DIctionary Server.一个基于KV的持久化分布式数据库.所编写的语言为C.与另一个分布式缓存Memcached有几分相似 但是redis ...

  6. Mac安装Mysql-python遇到的坑,被这俩报错反复摩擦:'my_config.h' file not found 和 IndexError: string index out of range

    最后Stackoverflow上面的大神解决了问题: Link brew install mysql brew unlink mysql brew install mysql-connector-c ...

  7. go微服务框架kratos学习笔记八 (kratos的依赖注入)

    目录 go微服务框架kratos学习笔记八(kratos的依赖注入) 什么是依赖注入 google wire kratos中的wire Providers injector(注入器) Binding ...

  8. KVM管理工具webvirtmgr的使用

    WebVirtMgr的日常配置:添加宿主机,创建虚拟机,磁盘扩容,快照等具体操作记录如下: 一.创建虚拟机 1.创建存储池 点击创建的宿主机,进入虚拟机部署界面 点击“存储池”按钮,创建存储池(即创建 ...

  9. Vue生命周期和钩子函数及使用keeplive缓存页面不重新加载

    Vue生命周期 每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期,在这个过程中会有一些钩子函数会得到回调 Vue中能够被网页直接使用的最小单位就是组件,我们经常写的: ...

  10. js模拟post提交表单

    function post(URL, PARAMS) {            var temp = document.createElement("form");         ...