zoukankan      html  css  js  c++  java
  • 洛谷P1102 A-B数对

    洛谷P1102 A-B数对

    https://www.luogu.org/problem/show?pid=1102

    题目描述

    出题是一件痛苦的事情!

    题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈!

    好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A-B=C的数对的个数。(不同位置的数字一样的数对算不同的数对)

    输入输出格式

    输入格式:

    第一行包括2个非负整数N和C,中间用空格隔开。

    第二行有N个整数,中间用空格隔开,作为要求处理的那串数。

    输出格式:

    输出一行,表示该串数中包含的所有满足A-B=C的数对的个数。

    对于90%的数据,N <= 2000;

    对于100%的数据,N <= 200000。

    所有输入数据都在longint范围内。

    输入输出样例

    输入样例#1:
    4 1
    1 1 2 3
    
    输出样例#1:
    3
    为什么用代码中注释掉的hash方式不对呢?
    求助路过大佬
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define in long long
    #define mo 20047
    #define mo2 13831
    using namespace std;
    struct node
    {
        in cs;
        in next;
        in to;
    }edge[2000001];
    in ans,tot,head[2000001],a[200001],i,j,n,c,b;
    in qr()
    {
        in x=1,f=0;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-') x=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            f=f*10+(in)ch-48;
            ch=getchar();
        }
        return x*f;
    }
    in get_hash1(in k)
    {
        /*in h=0;
        while(k)
        {
            h=(h*33+k)%mo;
            k/=10;
        }*/
        return k*1007%mo;
    }
    in get_hash2(in q)
    {
        /*in h=0;
        while(q)
        {
            h=(h*53+q)%mo2;
            q/=10; 
        }*/
        return q*1009%mo2;
    }
    void lj(in from,in to)
    { 
        for(int l=head[from];l;l=edge[l].next)
        {
            if(edge[l].to==to)
            {
                edge[l].cs++;
                return;
            }
        }
        tot++;
        edge[tot].next=head[from];
        edge[tot].to=to;
        head[from]=tot;
        edge[tot].cs++;
    }
    void add(in u,in v)
    {
        if(head[u])
        lj(u,v);
        else
        {
            tot++;
            edge[tot].next=head[u];
            edge[tot].to=v;
            head[u]=tot;
            edge[tot].cs++;
        }
    }
    int query(in u,in v)
    {
        for(j=head[u];j;j=edge[j].next)
        {
            if(edge[j].to==v)
            return edge[j].cs;
        }
        return 0;
    }
    int main()
    {
        n=qr();c=qr();
        for(i=0;i<n;++i)
        {
            a[i]=qr();
            in x=get_hash1(a[i]);
            in y=get_hash2(a[i]);
            add(x,y);
        }
        for(i=0;i<n;++i)
        {
            b=c+a[i];
            in y=get_hash1(b);
            in z=get_hash2(b);
            ans+=query(y,z);
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    [na]ip数据包格式
    [js]浏览器同源策略(same-origin policy)
    [sql] 同库表(结构)的备份和sql聚合&navicat使用
    [svc]tcp三次握手四次挥手&tcp的11种状态(半连接)&tcp的time-wait
    [svc]ip地址划分
    [css]单/多行居中&字体设置
    时间戳转为C#格式时间
    windows 8 中 使用 httpclient
    oralce 查看是否启动 登陆 创建用户 常用命令小记
    SQL递归查询(with cte as)
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6370487.html
Copyright © 2011-2022 走看看