题干:

DreamGrid has an integer sequence  and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements  and  () in the sequence when DreamGrid wasn't at home. When DreamGrid comes back, he finds with dismay that his precious sequence has been changed into !

What's worse is that DreamGrid cannot remember his precious sequence. What he only remembers are the two values

Given the sequence after swapping and the two values DreamGrid remembers, please help DreamGrid count the number of possible element pairs  BaoBao swaps.

Note that as DreamGrid is poor at memorizing numbers, the value of  or  might not match the sequence, and no possible element pair can be found in this situation.

Two element pairs  () and  () are considered different if  or .

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains three integers ,  and  (), indicating the length of the sequence and the two values DreamGrid remembers.

The second line contains  integers  (), indicating the sequence after swapping. It's guaranteed that  and .

It's guaranteed that the sum of  of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the number of possible element pairs BaoBao swaps.

Sample Input

2
6 61 237
1 1 4 5 1 4
3 20190429 92409102
1 2 3

Sample Output

2
0

Hint

For the first sample test case, it’s possible that BaoBao swaps the 2nd and the 3rd element, or the 5th and the 6th element.

解题报告:

两数作差不难发现,y的差除以x的差,就是a[i]+a[j],,所以分情况讨论就行了。注意除数为0的情况和x相同y不相同的时候(好坑啊、、)

(%%%wjh大佬)

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
//ll f[1000];
ll a[200000],b[200000];
map<ll,ll> mp;
map<ll,ll> ::iterator it;
int main()
{int t;cin>>t;while(t--){mp.clear();ll n,x,y;scanf("%lld%lld%lld",&n,&x,&y);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);mp[a[i]]++;b[i]=a[i]*a[i];}ll x2=0,y2=0;for(ll i=1;i<=n;i++){x2+=a[i]*i;y2+=b[i]*i;}if(x==x2){if(y!=y2){puts("0");continue;} ll ans=0;for(it=mp.begin();it!=mp.end();it++){ll num=it->second;ans+=num*(num-1)/2;}printf("%lld\n",ans);continue;}ll ans=0;if(abs(y-y2)%abs(x-x2)!=0){puts("0");continue;}ll tmp=(y-y2)/(x-x2);ll dx=x-x2;ll dy=y-y2;for(int i=1;i<=n;i++){ll aj=tmp-a[i];ll dt=a[i]-aj;ll dij;if(dt==0){continue;}if(abs(dx)%abs(dt)!=0)continue;dij=dx/dt;ll wj=i+dij;if(wj<=i) continue;if(aj==a[wj]&&(x==(x2+wj*a[i]+i*a[wj]-i*a[i]-wj*a[wj]))&&(y==(y2-i*b[i]-wj*b[wj]+wj*b[i]+i*b[wj])))ans++;elsecontinue;}printf("%lld\n",ans);}return 0;
}
/*
8
6 61 237
1 1 4 5 1 4
3 20190429 92409102
1 2 3
*/

【2019浙江省赛 - B】Element Swapping(思维,数学)相关推荐

  1. 【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

    题干: BaoBao has just found two strings  and  in his left pocket, where  indicates the -th character i ...

  2. 【2019浙江省赛 - E】Sequence in the Pocket(思维)

    题干: DreamGrid has just found an integer sequence  in his right pocket. As DreamGrid is bored, he dec ...

  3. 【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)

    题干: DreamGrid has just found an undirected simple graph with  vertices and no edges (that's to say, ...

  4. 2019 浙江省赛部分题解(The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    签到题 GLucky 7 in the Pocket Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao loves number 7 bu ...

  5. 【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)

    题干: The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be he ...

  6. 【数学】Element Swapping

    Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2 ...

  7. 2019电赛总结(序)

    2019电赛总结(序) 文章目录 2019电赛总结(序) 1 序 2 嵌入式与单片机省赛落败 3 电赛启航 4 PID简述 5 那之后 电赛的一些经验 1 序 鄙人于某工大集成电路设计专业就学,但电子 ...

  8. 2022浙江省赛、ICPC昆明区域赛 游·寄

    前夜 周六打的浙江省赛.周日昆明区域赛,周五平常作息,早上和爸妈聊天 我说道 金华疫情情况还好,还能出校吃饭,没想到 噩耗马上就来了.金华突然有了几例阳性,其中有一位还是滴滴司机.线上教学的消息已发出 ...

  9. java 分班_J 分班(class)(NYIST 2019年校赛)

    J 分班(class)(NYIST 2019年校赛) 内存限制:256MB 时间限制:1s Special Judge: No 题目描述: jsb 是 XX 市第一中学的校长.一轮模拟考试结束后,js ...

最新文章

  1. 轰动程序员圈的大事:女程序员将代码写到退休,返聘再续传奇
  2. MySQL出现同步延迟有哪些原因?如何解决?
  3. linux下编译wpa_supplicantnbsp;…
  4. Linux 库函数与系统调用的关系与区别
  5. 数据库系统(二)——数据更新和视图
  6. 前端学习(2743):重读vue电商网站53之项目上线
  7. Struts2、SpringMVC、Servlet(Jsp)性能对比 测试 。 Servlet的性能应该是最好的,可以做为参考基准,其它测试都要向它看齐,参照...
  8. Hadoop和Spark的异同
  9. 2012,字王强势回归
  10. 4 angular 重构 项目_再遇angular(angular4项目实战指南)
  11. jquery 逗号分割截取字符串_经典面试题:分割回文串
  12. Struts2返回JSON数据的具体应用范例
  13. UE4 命令行创建Pak
  14. java程序的运行结果依赖操作系统吗_SunJava程序员认证考试试题
  15. 什么是FBO (Frame Buffer Object)
  16. 466. 回文日期 Java题解 (模拟)
  17. 2022 ICPC Gran Premio de Mexico 1ra Fecha(一)
  18. 4.3实训任务 Zookeeper安装与配置
  19. biopython--PDB.polypepide
  20. Android 新手引导添加View的方法

热门文章

  1. 混合代码块 Markdown Leedcde
  2. 解不等式之代数和不等式
  3. linux vma,内存管理 – Linux内核API find_vma
  4. PHP与MySQL案例剖析_PHP与MySQL案例剖析
  5. win10进程太多怎么优化_用过最好用的Win10优化软件,全方面优化和管理!
  6. 7-7 汉密尔顿回路 (25 分)(C语言实现)
  7. c 的word转为html5,word与html互转(1) -- word转html
  8. Android P 网络报错 : java.io.IOException: Cleartext HTTP traffic to ***.com not permitted
  9. jquer each 遍历的结果不显示 null_SpringBoot系列(三十一)- Thymeleaf如何用th:each 做条件遍历
  10. Django框架里的MVC思想