B. Modulo Equality
当时想到的第一个想法是用拓展欧几里得解方程,求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的更多相关文章
- Codeforces Round #609 (Div. 2) 题解
Equation Modulo Equality Long Beautiful Integer Domino for Young K Integers Equation \[ Time Limit: ...
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- Codeforces Round #609 (Div. 2) A-E简要题解
contest链接:https://codeforces.com/contest/1269 A. Equation 题意:输入一个整数,找到一个a,一个b,使得a-b=n,切a,b都是合数 思路:合数 ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
- Determining Equality of Objects
[Determining Equality of Objects] If you need to determine whether one object is the same as another ...
- 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/ ...
随机推荐
- SSH(一)
系统程序架构: 整合思路 1.逆依赖方向而行,由Spring提供对象管理和服务 2.依次实现Spring与Hibernate.Spring与Struts2的集成 配置 ...
- GDI+如何判断一个点是否在区域内
https://msdn.microsoft.com/en-us/library/windows/desktop/ms533826(v=vs.85).aspx The purpose of hit t ...
- sizeof('a')
#include <iostream> using namespace std; int main(void) { cout << sizeof('a') << e ...
- JAVA中如何获取变量的类型
JAVA中如何获取变量的类型? package xiya; public class Demo { public static void main(String[] args) { String ty ...
- Grafana & Graphite & Collectd:监控系统
简介 监控是运维工作中的一个重要组成部分,今天介绍一套新的监控工具,方便好用,扩展性强,这套工具有三个组件,Grafana & Graphite & Collectd: Grafana ...
- 【redis】-- redis的持久化(作为数据库)
目录 1.RDB rdb持久化的方式 rdb方式的优点: aof的优点 3.持久化的其他特性 日志重写 工作原理 rdb和aof混合使用 redis是一个基于内存的数据库,故在redis正在运行的数据 ...
- 《C# GDI+ 破境之道》:第一境 GDI+基础 —— 第三节:画圆形
有了上一节画矩形的基础,画圆形就不要太轻松+EZ:)所以,本节在画边线及填充上,就不做过多的讲解了,关注一下画“随机椭圆”.“正圆”.“路径填充”的具体实现就好.与画矩形相比较,画椭圆与之完全一致,没 ...
- 如何用Python实现do...while语句
我在编程的时候可能会遇到如下代码: a = 0 while a != 0: a = input() print a 我所设想的运行过程是这样的: 很显然我是想先运行后判断的模式,即 do...whil ...
- 珠峰-vue
- mac下搭建http服务器(apache+php),使用homebrew升级php
新版mac依旧预装了 Apache ,但是已经不能在 「系统偏好设置」中的「Web 共享」来开启了,需要手动通过命令行开启. 启动Apache 启动:sudo apachectl start 停止:s ...