CF447A DZY Loves Hash 模拟
CF447A DZY Loves Hash 模拟
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a by b.
However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.
The first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi(0 ≤ xi ≤ 109).
Output a single integer — the answer to the problem.
10 5021534153
4
5 501234
-1就是问hash是否存在冲突;用 map 去判重即可;当然开数组也没问题;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 20005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {ll x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x;
}ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans;
}
*/int n, p;
int x[maxn];map<int, int>mp;
int main() {//ios::sync_with_stdio(0);cin >> p >> n;int pos = -1;for (int i = 1; i <= n; i++)cin >> x[i];for (int i = 1; i <= n; i++) {int tmp = x[i];if (!mp[tmp%p]) {mp[tmp%p] = 1;}else if (mp[tmp%p]) {pos = i; break;}}cout << pos << endl;return 0;
}
CF447A DZY Loves Hash 模拟相关推荐
- CF A. DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #FF (Div. 2) A.DYZ Loves Hash
水题.题目大意为输入一个p,n,再输入n组数据,每组数据对p取余,当和前面相同时,发现冲突,记下第几组,找到最后一组没有找到和前面一样的输出-1. *可以采用一个标记数组,就变得很简单了. A. DZ ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
- BZOJ 3309 DZY Loves Math
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- HDU 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- 【BZOJ3512】DZY Loves Math IV(杜教筛)
[BZOJ3512]DZY Loves Math IV(杜教筛) https://www.cnblogs.com/cjyyb/p/10165338.html
- Codeforces 446C. DZY Loves Fibonacci Numbers【斐波那契+线段树】
C. DZY Loves Fibonacci Numbers [题目描述] 传送门 [题解] 我们可以知道斐波那契数列有两个性质: ∑i=1nFi=Fn+2−F2\sum_{i=1}^{n} F_i= ...
最新文章
- oracle 数据有引号,oracle插入字符串数据时字符串中有'单引号问题
- 图片底下配的文字叫什么_38岁孙俪越来越有女人味!穿短裙配性感黑丝美腿撩人,短发超美...
- ML大杂烩:**常见机器学习算法公式梳理
- VS 2005 安装一个的小问题
- leetcode403 青蛙过河
- java.io 相关tips
- 关于CSS兼容IE与Firefox要点分析
- C# socket编程TcpClient与TcpListener UdpClient
- android-腾讯街景已经百度街景对比
- 275. H指数 II
- python geopy 北斗导航_【Python】使用geopy由经纬度找地理信息
- mysql工作时间获取_mysql获取当前时间,及其相关操作
- linux_Flatpak
- oracle怎样修改口令,修改Oracle数据库sys口令
- matlab+字体设置大小,Matlab中如何修改字体的大小?
- linux双系统没有wifi,遇到了双系统中win下关闭了wifi,ubuntu中也无法启用的情况...
- 2018 BUPT Winter Training #3 Div.2
- LOJ6482. LJJ 爱数数
- 浏览器兼容性问题(WEB项目中页面)
- 用电脑发传真用无纸传真系统3GFAXorMYFAX
热门文章
- VS 2010错误提醒: 必需属性“VSInstallDir”缺失或为空 以及 error MSB8008
- Unity分屏显示效果
- echarts动态legend不变更
- Floyd是咋求图的最短路径?
- JavaWeb之JavaScript及应用
- C++编译错误提示 [Error] name lookup of 'i' changed for ISO 'for' scoping
- js连接蓝牙打印机打印一维码和二维码
- java写netcdf_[转]netcdf入门
- c语言txt播放器,c语言播放器更新版
- UVM--Sequencer和Sequence