Vanya and Field

Vanya and Field

题目链接:http://www.codeforces.com/problemset/problem/492/E

逆元

刚看到这题的时候一脸懵逼不知道从哪下手好,于是打表找规律。但是打出来的东西完全不能看啊,有个鬼规律(╯‵□′)╯︵┻━┻,是我数据处理不当?按x排序后发现从一个点出发可以到达任意一个x坐标,回过去看题,发现有这么一句话:The following condition is satisfied for the vector: ,恩,好像有点思路了。(x,y)的下一个点的坐标是[(x+k*dx)%n,(y+k*dy)%n]也可以写作[(x+k*dx+a*n),(y+k*dy+b*n)],也就是说每个不互相覆盖的(x,y)都唯一对应一个(0,y‘),利用扩展欧几里得(如果n是质数可以用费马小定理)计算dx对n的逆元,从而可以求得对应的y‘。于是只要处理每个(x,y),对应的(0,y‘)个数++,最后找到个数最多的(0,y‘)就好了。

代码如下:

 1 #include<cstdio>
 2 #define LL long long
 3 using namespace std;
 4 LL k,t;
 5 struct node{
 6     LL sum,x,y;
 7     bool flag;
 8 }times[1000005];
 9 LL query[100005][2];
10 LL n,m,dx,dy;
11 LL exGCD(LL a,LL b){
12     if(b==0){
13         k=1;
14         t=0;
15         return a;
16     }
17     LL r=exGCD(b,a%b);
18     LL tmp=k;
19     k=t;
20     t=tmp-(a/b)*t;
21     return r;
22 }
23 int main(void){
24     LL Max=0,Index=0;
25     scanf("%I64d%I64d%I64d%I64d",&n,&m,&dx,&dy);
26     exGCD(dx,n);
27     k=(k%n+n)%n;
28     for(LL i=0;i<m;++i){
29         LL x,y;
30         scanf("%I64d%I64d",&x,&y);
31         query[i][0]=x,query[i][1]=y;
32         LL temp=y-x*k*dy;
33         temp = (temp%n + n)%n;
34         temp%=n;
35         times[temp].sum++;
36         if(times[temp].sum>Max){
37             Max=times[temp].sum;
38             Index=temp;
39         }
40         if(times[temp].flag==0){
41             times[temp].x=x;
42             times[temp].y=y;
43             times[temp].flag=1;
44         }
45     }
46     printf("%I64d %I64d\n",times[Index].x,times[Index].y);
47     return 0;
48 }
时间: 2024-08-22 09:07:17

Vanya and Field的相关文章

codeforces 492E. Vanya and Field(exgcd求逆元)

题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同. 所以,x肯定要经过0点,所以我只需要求y点就可以了. i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M. 则有 1----------------------(i + b * dx) % n = 0 2------

Codeforces 492E Vanya and Field(拓展欧几里得)

题目链接:Codeforces 492E Vanya and Field 通过拓展欧几里得算法求出每个位置在移动过程中,在x为0时,y的位置.统计相应y坐标最多的即为答案. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e6 + 5; const int maxm = 1e5 + 5; typedef long long l

【CF492E】【数学】Vanya and Field

Vanya decided to walk in the field of size n × n cells. The field contains m apple trees, the i-th apple tree is at the cell with coordinates (xi, yi). Vanya moves towards vector (dx, dy). That means that if Vanya is now at the cell (x, y), then in a

CF492E Vanya and Field

题目大意: 一天,Vanya 来到了一片滑稽树林中,准备采摘滑稽果.滑稽树林可看作一个 n×n 的正方形,其中 m 个点上有滑稽果,某些点没有.(注意一个点上可能有多个滑稽果) Vanya会沿向量 (dx,dy)(dx,dy) 方向移动.也就是说,如果Vanya在 (x,y) 处,她将移动到 ((x+dx) mod n,(y+dy) mod n)((x+dx) mod n,(y+dy) mod n) 处.每到一个点,她会拿走该点所有滑稽果.(dx,dy 为定值(输入中给出),且 gcd(n,dx

Codeforces 492E Vanya and Field 规律题

题目链接:点击打开链接 给定n*n的矩阵(0,0)->(n-1, n-1) m个苹果(下面m行给出苹果坐标)(dx, dy) 向量. 任选一个起点,用这个向量在矩阵里跑,问最多能采摘多少个苹果(坐标是%n, 即超过矩阵时 (x%n, y%n)) 输出起点. 思路: 把向量所在的点集写出来会发现一个起点一定经过了n个点,即至多只有n种起点 所以把点分成n个组即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #in

CodeForces 492E Vanya and Field

题意: n*n的矩形内有m(10^5)个点  从任一点开始以速度向量(dx,dy)移动  直到走到曾经走过的位置  问  从哪里开始  可以经过最多的点数  dx和dy均与n互素 思路: 很容易想到以dx,dy移动的话  走的一定是一个圈  不会是出现"蝌蚪形"然后循环  注意题意最后一句  而且每个x坐标一定只经过一次 那么对于m个点  我们可以求出它是(0,y)这个点可以到达的  求解方法就是用拓展欧几里德解下面方程 (x+dx*k)%mod=xi , (y+dy*k)%mod=y

Codeforces Round #280 (Div. 2)

A. Vanya and Cubes 手速不够快,被别人抢先了... #include<bits/stdc++.h> #define eps 1e-9 #define FOR(i,j,k) for(int i=j;i<=k;i++) #define MAXN 1005 #define MAXM 40005 #define INF 0x3fffffff using namespace std; typedef long long LL; int i,j,k,n,m,x,y,T,ans,bi

Codeforces Round #280 (Div. 2) 解题报告 A.B.C.D.E.

不知道到底是我的水平提高了还是CF的题目变水了...... A - Vanya and Cubes 水题..暴力枚举就可以.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include &l

Codeforces Round #280 (Div. 2 A,B,C,D,E)

改了时区之后打cf更辛苦了啊...昨天没做,今天补了一下啊. A. Vanya and Cubes 每次加的数规律性很明显就是:(i+1)*i/2.暴力枚举i就可以得到答案. #include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <