poj3349
来源:互联网 发布:中国网络黑客事件 编辑:程序博客网 时间:2024/05/16 15:05
题意:
给你n个六个花瓣的雪莲,问你有没有两个是相同的(圆形,可以转换位置)。有输出Twin snowflakes found.不然输出No two snowflakes are alike.
n<100000;花瓣0~10000000;
思路:
用hash,用每个花瓣的平方和mod作为key。找以前有没有出现过,有就是说明有两个是相同的,pascal的记得key要用qword。
时间复杂度:
基本上等于O(n),但是平方什么的,还有hash,常数比较大所以差不多可以卡过
`const
maxn=9999877;
var
a:array [1..6] of longint;
hash:array [0..maxn] of longint;
i,j,n,m:longint;
function find:boolean;
var
j,k:longint;
i:qword;
begin
k:=a[1]+a[2]+a[3]+a[4]+a[5]+a[6];
i:=sqr(a[1]) mod maxn;
i:=(i+sqr(a[2])) mod maxn;
i:=(i+sqr(a[3])) mod maxn;
i:=(i+sqr(a[4])) mod maxn;
i:=(i+sqr(a[5])) mod maxn;
i:=(i+sqr(a[6])) mod maxn;
while (hash[i]<>-1) and (hash[i]<>k) do i:=(i+1) mod maxn;
if hash[i]=k then exit(true);
hash[i]:=k;
exit(false);
end;
begin
readln(n);
fillchar(hash,sizeof(hash),255);
for i:=1 to n do
begin
for j:=1 to 6 do
read(a[j]);
readln;
if find then
begin
writeln(‘Twin snowflakes found.’);
halt;
end;
end;
writeln(‘No two snowflakes are alike.’);
end.
`
- poj3349
- poj3349
- POJ3349
- POJ3349
- poj3349
- poj3349
- POJ3349
- POJ3349
- poj3349
- poj3349
- POJ3349
- poj3349
- POJ3349
- poj3349
- POJ3349 哈希表
- POJ3349 #no
- poj3349-hash
- poj3349--Hash
- get post请求乱码问题
- epoch和iteration的区别
- Java方法重载
- eclipse中常用快捷键使用
- NYOJ-看美女
- poj3349
- 什么是hibernate懒加载?什么时候用懒加载?为什么要用懒加载?
- LruMemChunk SharedMemCache::getLru().end()
- 解开 Windows 下的临界区中的代码死锁
- io流
- Mysql-索引覆盖
- 20 Python __all__
- Python深入02 上下文管理器
- 整理nodejs中的exports 和 module.exports的关系