Codeforces #280 Div.2 E.Vanya and Field 数学,数论

第一道完全没看题解直接出的E题……纪念一下记下来吧……

题意:n*n的矩阵,边际循环,每次可以从当前位置(x,y)走到((x+dx)%n,(y+dy)%n),保证gcd(n,dx)=1,gcd(n,dy)=1;

在矩阵中有些地方有果树,问从哪个位置开始走能够经过的果树最多;


题解:考虑能够从一棵果树走到另一棵,设这两个果树的坐标为(x1,y1),(x2,y2),那么我们可以得到

(x2-x1)/dx=(y2-y1)/dy (mod n)

两边乘以dx*dy,可以得到

(x2-x1)*dy=(y2-y1)*dx (mod n)

整理一下,

x1*dy-y1*dx=x2*dy-y2*dx (mod n)

所以只需要开一个map或者大小为n的数组,记录下每个果树的(x*dy-y*dx)%n值,在记录的时候如果比当前值大就更新答案就行了,代码非常短……


/***********************************************
Made by Euphoria.
************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<cassert>

#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<set>
#include<algorithm>
#include<stack>
#include<list>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int ui;
typedef long double ld;
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 0x7fffffff
#define xx first
#define yy second
#define PI acos(-1.0)
#define eps 1e-8
#define MOD 1000000007

#define N 100001

map<ll, int>M;

int main()
{
	ll n, m, dx, dy;
	cin >> n >> m;
	cin >> dx >> dy;
	ll cnt = 0;
	pii ans;
	for (int i = 1; i <= m; i++)
	{
		ll a, b;
		cin >> a >> b;
		ll tmp = ((a * dy - b * dx) % n + n) % n;
		M[tmp]++;
		if (cnt < M[tmp] )
		{
			cnt = M[tmp];
			ans.xx = a; ans.yy = b;
		}
	}
	cout << ans.xx << ' ' << ans.yy << endl;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值