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/ ...
随机推荐
- ROS机器人话题之自定义消息
ROS提供了丰富的内建消息,std_msgs包定义了一些基本的类型. 具体例子 首先定义一个消息类型的文件叫做Complex 例Complex.msg float32 real float32 ima ...
- 死磕java(7)
http://www.cnblogs.com/liunanjava/p/4296045.html 自己写的例子 package com.sougn.trynew; public abstract cl ...
- 牛客练习赛34 D little w and Exchange(归纳)
题意: 给n个数,和m 问这组数是否可以构成[1, m]中的每一个数 思路: 先将a数组排序. 先算算构成前几个数需要什么,至少需要a[1]=1 需要a[2] = 1,2 在a[2] = 1的情况下a ...
- 转载 angularJS filter 过滤器
angularjs中的filter(过滤器) 标签: angularjsfilter 源文地址:http://www.ncloud.hk/技术分享/angularjs中的filter-过滤器/ f ...
- TTStand 基础知识[8] Build-In StepTypes(3)
Build-In Step Types的最后一篇,前面两篇的连接如下: TestStand 基础知识[7] Build-In StepTypes(2) TestStand 基础知识[6] Build- ...
- 从敏捷开发到微服务,maybe再到中台
-- 先说下准备这个的背景: 本来是想让我分享下敏捷开发,可能是听我说为as**搭建并完善了敏捷开发体系的原因吧. 我一般分享一个东西,希望大家能真的理解,而不只是知道. 我不大相信有万能的东西,不希 ...
- 浅谈JSONP 的工作原理
小编最近在工作中经常用到 jsonp 这个东西, 表示之前从来没用过 最近稍微研究了下 当然很多内容来源于网上 收集整理 你懂的 ~~~ 话说我们访问一个页面的时候 需要像另一个网站获取部分信息, ...
- AtCoder Beginner Contest 156
https://atcoder.jp/contests/abc156/tasks A - Beginner #include <bits/stdc++.h> #define ll long ...
- H5谷歌登录 webview错误
接入谷歌登录 H5接入谷歌登录打包apk使用webview谷歌登录不上(谷歌不支持用户代理被归类为嵌入式用户代理(也称为网络视图),因此不允许向Google发出OAuth授权请求) 解决办法:替换ua ...
- android应用开发错误:Your project contains error(s),please fix them before running your
重新打开ECLIPSE运行android项目,或者一段时间为运行ECLIPSE,打开后,发现新建项目都有红叉,以前的项目重新编译也有这问题,上网搜索按下面操作解决了问题 工程上有红叉,不知道少了什么, ...