P1102 A-B数对

题目描述

出题是一件痛苦的事情!
相同的题目看多了也会有审美疲劳,于是我舍弃了大家所熟悉的 A+B Problem,改用 A-B 了哈哈!
好吧,题目是这样的:给出一串数以及一个数字 C C C,要求计算出所有 A − B = C A - B = C AB=C 的数对的个数(不同位置的数字一样的数对算不同的数对)。

输入格式

输入共两行。
第一行,两个整数 N , C N, C N,C
第二行, N N N 个整数,作为要求处理的那串数。

输出格式

一行,表示该串数中包含的满足 A − B = C A - B = C AB=C 的数对的个数。

思路

hash就可以了
A-B=C->A+C=B
code:

#include<cstdio>
#include<queue>
#include<iostream>
#include<algorithm>
#define myd 1000007
using namespace std;
long long a[myd][2],tot;
long long n,m,s,ans;
inline long long ip()
{
    
    
 char c=getchar();
 long long ans=0;
 while (!isdigit(c)) c=getchar();
 while (isdigit(c)) ans=ans*10+c-48,c=getchar();
    return ans;
}
long long f(long long x)
{
    
    
 long long o=x;
 o%=myd;
 long long i=0;
 while (i<myd&&a[(o+i)][0]!=x&&a[(o+i)%myd][0]) i++;
 return (o+i)%myd;
}
bool find(long long x)
{
    
    
 return a[f(x)][0]==x;
}
void cr(long long x)
{
    
    
 a[f(x)][0]=x;
 a[f(x)][1]++;
 return;
}
long long x,i,c;
int main()
{
    
    
 n=ip(),c=ip();
 for (i=1;i<=n;i++)
 {
    
    
  x=ip();
  cr(x);
 }
 for (i=0;i<myd;i++)
 {
    
    
  s+=a[i][1]*a[f(a[i][0]+c)][1];
 }
 cout<<s;
 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/112970099
A-B