【數學】Element Swapping

Element Swapping
Time Limit: 1 Second Memory Limit: 65536 KB

DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1?,a2?,a3?,,an? and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai? and aja_jaj? (1≤i&lt;j≤n)(1 \le i &lt; j \le n)(1i<jn) 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 a1,a2,…,ai?1,aj,ai+1,…,aj?1,ai,aj+1,…,ana_1,a_2,\dots,a_{i-1},a_j,a_{i+1},\dots,a_{j-1},a_i,a_{j+1},\dots,a_na1?,a2?,,ai?1?,aj?,ai+1?,,aj?1?,ai?,aj+1?,,an?

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

x=∑k=1nkakandy=∑k=1nkak2\ x = \sum_{k=1}^nka_k \qquad \text{and} \qquad y = \sum_{k=1}^nka_k^2?x=k=1n?kak?andy=k=1n?kak2?

Given the sequence after swapping and the two values DreamGrid remembers, please help DreamGrid count the number of possible element pairs (ai,aj)(a_i,a_j)(ai?,aj?) 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 (ai,aj)(1≤i&lt;j≤n)(a_i,a_j)(1 \le i &lt; j \le n)(ai?,aj?)(1i<jn) and (ap,aq)(1≤p&lt;q≤n)(a_p,a_q)(1 \le p &lt; q \le n)(ap?,aq?)(1p<qn) are considered different if i=?pi =\not pi=??p or j=?qj =\not qj=??q.

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

The first line contains three integers nnn,xxx and yyy (2≤n≤105,1≤x,y≤1018)(2 \le n \le 10^5,1 \le x,y \le 10^{18})(2n105,1x,y1018), indicating the length of the sequence and the two values DreamGrid remembers.

The second line contains nnn integers b1,b2,…,bn(1≤bi≤105)b_1,b_2,\dots,b_n(1 \le b_i \le 10^5)b1?,b2?,,bn?(1bi?105), indicating the sequence after swapping. It’s guaranteed that ∑k=1nkbk≤1018\sum_{k=1}^nkb_k \le 10^{18}k=1n?kbk?1018 and ∑k=1nkbk2≤1018\sum_{k=1}^nkb_k^2 \le 10^{18}k=1n?kbk2?1018.

It’s guaranteed that the sum of of all test cases will not exceed 2×1062 \times 10^62×106.

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.

題目大意:
第一行輸入一個整數ttt,代表有ttt組測試樣例,對于每組測試樣例,先輸入三個整數n,x1,y1n,x_1,y_1n,x1?,y1?,下一行再輸入nnn個整數的數組a1,a2,…,ana_1,a_2,\dots,a_na1?,a2?,,an?(1≤ai≤105)(1 \le a_i \le 10^5)(1ai?105),現在我們能交換數組中的任意兩個元素aia_iai?aja_jaj? (1≤i&lt;j≤n)(1 \le i &lt; j \le n)(1i<jn),使得改變后的數組求得的x,yx,yx,y變為x1,y1x1,y1x1,y1,問有多少種交換方案。
解題思路:
通過題意我們可以知道,此題相當于我們可以根據公式x=∑k=1nkakx=\sum_{k=1}^nka_kx=k=1n?kak? 和 公式y=∑k=1nkak2y=\sum_{k=1}^nka_k^2y=k=1n?kak2?求出當前數組的xxxyyy,我們可以將其記作x2,y2x_2,y_2x2?,y2?,交換數組中的兩個元素后的x,yx,yx,yx1,y2x_1,y_2x1?,y2?,因為我們僅交換了數組中的兩個元素,所以根據x,yx,yx,y的公式,我們可以知道x1,x2x_1,x_2x1?,x2?的區別在于x2x2x2ai,aja_i,a_jai?,aj?兩項為iai+jajia_i +ja_jiai?+jaj?,而x1x1x1也就是交換后ai,aja_i,a_jai?,aj?兩項為jai+iajja_i +ia_jjai?+iaj?,其他項完全相同,所以可以得到x2?x1=(i?j)(ai?aj)x2-x1=(i-j)(a_i-a_j)x2?x1=(i?j)(ai??aj?),同理y2?y1=(i?j)(ai2?aj2)y2-y1=(i-j)(a_i^2-a_j^2)y2?y1=(i?j)(ai2??aj2?)y2?y1=(i?j)(ai?aj)(ai+aj)y_2-y_1=(i-j)(a_i-a_j)(a_i+a_j)y2??y1?=(i?j)(ai??aj?)(ai?+aj?)
因此可以先假設x=x2?x1,t=y2?y1x=x_2-x_1,t=y_2-y_1x=x2??x1?,t=y2??y1?,如果x==0&amp;&amp;y==0x==0\&amp;\&amp;y==0x==0&&y==0的話,那么則證明ai==aja_i==a_jai?==aj?,則方案數則為數組中選取任意兩個相同項的方案數,例如假設aaa在數組中出現了kkk次,則ans+=Ck2ans+=C_k^2ans+=Ck2?
x=?0&amp;&amp;y=?0x=\not 0 \&amp;\&amp; y=\not 0x=??0&&y=??0,通過x=(i?j)(ai?aj)x=(i-j)(a_i-a_j)x=(i?j)(ai??aj?)y=(i?1)(ai?aj)(ai+aj)y=(i-1)(a_i-a_j)(a_i+a_j)y=(i?1)(ai??aj?)(ai?+aj?),可知y/x=ai+ajy/x=a_i+a_jy/x=ai?+aj?,這是就可以判斷一下若y%x==0y\%x==0y%x==0則存在,否則ans=0ans=0ans=0,因此可以得到ai+aja_i+a_jai?+aj?的值,此時就可以通過遍歷數組a[]a[]a[],求出其對應的a[j]a[j]a[j],通過遍歷可以確定i,a[i],a[j]i,a[i],a[j]i,a[i],a[j]的值,然后根據x=(i?j)(ai?aj)x=(i-j)(a_i-a_j)x=(i?j)(ai??aj?),可以求出jjj,這時只需判斷數組中的第jjj項是否為a[j]a[j]a[j],若是則方案數加一。
代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
ll arr[100100];
ll num[100100];
set<ll> s;
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {int n;ll x1,x2=0,y1,y2=0;scanf("%d %lld %lld",&n,&x1,&y1);s.clear();memset(num,0,sizeof num);rep(i,1,n) {scanf("%lld",&arr[i]);x2+=(ll)i*arr[i];y2+=(ll)i*arr[i]*arr[i];s.insert(arr[i]);num[arr[i]]++;}ll x=x2-x1,y=y2-y1;ll ans=0;if(x==0&&y==0) {for(auto it=s.begin();it!=s.end();it++) {if(num[*it]>1) {ans+=(num[*it]*(num[*it]-1))/2LL;}}}else if(x!=0&&y!=0) {if(y%x==0) {ll t=y/x;for(int i=1;i<=n;i++) {if(t-arr[i]>0&&t-arr[i]<=100000&&num[t-arr[i]]>0) {ll nape=arr[i]-(t-arr[i]);if(nape!=0&&x%nape==0) {int k=i-x/nape;if(k>i&&k<=n&&arr[k]==t-arr[i]) ans++;}}}}else ans=0;}else ans=0;printf("%lld\n",ans);}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536293.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536293.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536293.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【二分+二維前綴和】Largest Allowed Area

Largest Allowed Area 時間限制: 1 Sec 內存限制: 128 MB 提交: 146 解決: 54 [提交] [狀態] [命題人:admin] 題目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【數學】Floating-Point Hazard

Floating-Point Hazard 時間限制: 1 Sec 內存限制: 128 MB 提交: 106 解決: 42 [提交] [狀態] [命題人:admin] 題目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【manacher】Strings in the Pocket

Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ss1s2…snss_1s_2\dots s_nss1?s2?…sn? and tt1t2…tntt_1t_2\dots t_ntt1?t2?…tn? in his left pocket, where sis_isi? indicates the iii-th character in…

【并查集+dp】Team

Team 時間限制: 1 Sec 內存限制: 128 MB 提交: 124 解決: 10 [提交] [狀態] [命題人:admin] 題目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【線段樹】Segment Tree

Segment Tree 時間限制: 1 Sec 內存限制: 512 MB 提交: 107 解決: 23 [提交] [狀態] [命題人:admin] 題目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satis?ed that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

題目鏈接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 題目大意&#xff1a;Mario從第1站出發&#xff0c;目的是在時刻T會見車站 nnn 的一個間諜。由于在車站等待容易被抓&#xff0c;所以應盡量躲在開動的火車上&#xff0c;即在車站等待的時間最短&#xff0c;且Ma…

HDU1284——錢幣兌換問題【dp】

錢幣兌換問題 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一個國家僅有1分&#xff0c;2分&#xff0c;3分硬幣&#xff0c;將錢N兌換成硬…

HDU6092——Rikka with Subset 【dp】

題目網址&#xff1a; https://vjudge.net/problem/HDU-6092 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has nn positive A1?AnA1?An and their sum is mm…

洛谷P1725琪露諾【單調隊列+dp】

題目描述 在幻想鄉&#xff0c;琪露諾是以笨蛋聞名的冰之妖精。 某一天&#xff0c;琪露諾又在玩速凍青蛙&#xff0c;就是用冰把青蛙瞬間凍起來。但是這只青蛙比以往的要聰明許多&#xff0c;在琪露諾來之前就已經跑到了河的對岸。于是琪露諾決定到河岸去追青蛙。 小河可以看…

扶桑號戰列艦【RMQ+分治】

扶桑號戰列艦 時間限制: 1 Sec 內存限制: 128 MB Special Judge 提交: 197 解決: 63 [提交] [狀態] [命題人:admin] 題目描述 眾所周知&#xff0c;一戰過后&#xff0c;在世界列強建造超無畏級戰列艦的競爭之中&#xff0c;舊日本海軍根據“個艦優越主義”&#xff0c;建造了扶…

大鳳號裝甲空母【找規律+矩陣快速冪】

大鳳號裝甲空母 時間限制: 1 Sec 內存限制: 128 MB 提交: 108 解決: 15 [提交] [狀態] [命題人:admin] 題目描述 大鳳號航空母艦很喜歡算術。 它&#xff0c;是舊日本海軍中最為先進的航空母艦。 它&#xff0c;是舊日本海軍中最為短命的航空母艦。 同時&#xff0c;她還是最平…

codevs5429 多重背包【多重背包+單調隊列】

5429 多重背包 時間限制: 1 s 空間限制: 256000 KB 題目等級 : 鉆石 Diamond 題目描述 Description 你有一個容量為M的背包&#xff0c;和N種物品。 每種物品都有三個屬性&#xff0c;vi&#xff0c;wi&#xff0c;與ci&#xff0c;分別表示這種物品的體積、價值和件數。 你的…

generator 1【矩陣快速冪】

題目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0?,x1?,a,b. And you know xia?xi?1b?xi?2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi?a?xi?1?b?xi?2? for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速冪+歐拉】

Give Candies 時間限制: 1 Sec 內存限制: 128 MB 提交: 243 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找規律】

Save the Room 時間限制: 1 Sec 內存限制: 128 MB 提交: 149 解決: 90 [提交] [狀態] [命題人:admin] 題目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大數+二分】

Participate in E-sports 時間限制: 2 Sec 內存限制: 128 MB 提交: 194 解決: 53 [提交] [狀態] [命題人:admin] 題目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩陣快速冪】

Poor God Water 時間限制: 1 Sec 內存限制: 128 MB 提交: 102 解決: 50 [提交] [狀態] [命題人:admin] 題目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 時間限制: 1 Sec 內存限制: 128 MB 提交: 175 解決: 65 [提交] [狀態] [命題人:admin] 題目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…

洛谷P2015 二叉蘋果樹【樹形dp】

P2015 二叉蘋果樹 時間限制 1.00s 內存限制 125.00MB 題目描述 有一棵蘋果樹&#xff0c;如果樹枝有分叉&#xff0c;一定是分2叉&#xff08;就是說沒有只有1個兒子的結點&#xff09; 這棵樹共有N個結點&#xff08;葉子點或者樹枝分叉點&#xff09;&#xff0c;編號為1-N,…

洛谷P2014【樹形dp】

P2014 選課 時間限制 1.00s 內存限制 125.00MB 題目描述 在大學里每個學生&#xff0c;為了達到一定的學分&#xff0c;必須從很多課程里選擇一些課程來學習&#xff0c;在課程里有些課程必須在某些課程之前學習&#xff0c;如高等數學總是在其它課程之前學習。現在有N門功課&…