当时想到的第一个想法是用拓展欧几里得解方程,求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. 引入Activiti配置文件activiti.cfg.xml

    前面我们用代码实现了生成25张activiti表,今天我们用Activiti提供的activiti.cfg.xml配置文件来简化实现前面的功能: 官方文档参考地址:http://activiti.or ...

  2. Dubbox 环境搭建-新(Windows下)

    分布式服务框架 dubbo/dubbox 入门示例 dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http:// ...

  3. Nginx 缓存命中率

    # 在http头部显示命中方式 location ~* ^.*\.(js|ico|gif|jpg|jpeg|png)$ { proxy_redirect off; proxy_set_header H ...

  4. 编写windows服务程序

    2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...

  5. apache 访问日志access_log 配置和解析 rotatelogs分割日志

    一.解析访问日志        apache 的访问日志记载着大量的信息,学会高效快捷的读出其中关键信息对我们的工作有极大帮助.       如果Apache的安装方式是默认安装,服务器一运行就会有两 ...

  6. 为什么Linux 实例执行 df 和 du 查看磁盘时结果不一致

    问题现象 执行 df -h 查看 ECS Linux 实例文件系统使用率,可以看到 /dev/xvdb1 磁盘占用了约27G,挂载目录为 /opt . 进入到 /opt 目录执行 du -sh ,显示 ...

  7. zabbix-agentd配置文件详解

    agent 端配置文件路径 :/etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid #<===指定pi ...

  8. centos7.5优化系统脚本(虚拟机下)

    #!/usr/bin/bash #安装常用软件,首先必须自行调整好网卡配置文件,保证可以上网,否则,下列优化会失败 yum -y install wget vim lrzsz bash-complet ...

  9. 1213 - Fantasy of a Summation

    1213 - Fantasy of a Summation         If you think codes, eat codes then sometimes you may get stres ...

  10. MySQL常用关键词

    MySQL常用关键词 1.  显示表departments的结构:DESC DESC departments; 2. 显示出表employees中的全部job_id(不能重复):DISTINCT SE ...