You are given an array of size N. How many distinct arrays can you generate by swapping two numbers for exactly once? The two selected numbers can be equal but their positions in the array must be different.

Input

The first line of the input contains a single integer T, denoting the number of test cases. Every test case starts with an integer N. The next line contains N integers, the numbers of the array.

Output

For each tescase output the answer in a single line.

Constraints:

1 <= T <= 5

1 <= Value of a number in the array <= 100000

2 <= N <= 100000

Example

Input:

1
5
2 3 2 3 3

Output:
7

You can generate the following arrays:

2 3 2 3 3

2 2 3 3 3

2 3 3 2 3

2 3 3 3 2

3 2 2 3 3

3 3 2 2 3

3 3 2 3 2

 

题意:问给定一个数论,问交换两个位置上的数,可以变成多少个新的数组(重复的只统计一次)。

思路:先考虑变后与原来数论不同:对于每个位置,可以和与此数不同的位置交换,每一种合法交换统计了两次,最后除二。

如果某个数出现的次数大于1,则可以产生与原数论相同的数列。ans++;

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],num[maxn];
long long ans=;
int main()
{
int N,T,i,j; bool F;
scanf("%d",&T);
while(T--){
memset(num,,sizeof(num));
scanf("%d",&N); ans=; F=false;
for(i=;i<=N;i++){
scanf("%d",&a[i]);
num[a[i]]++;
}
for(i=;i<=N;i++){
if(num[a[i]]>) F=true;
ans+=(N-num[a[i]]);
}
ans>>=1LL;
if(F) ans++;
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. Java概念性问题
  2. 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用
  3. iOS 关于僵尸对象和僵尸指针的那些事儿
  4. 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
  5. inline(内联函数)
  6. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-007-给flowl加权限控制&lt;secured&gt;
  7. 关于cocos2d这个东西
  8. 私有静态方法private static method-值得用吗?
  9. Installation and Configuration MySQL Cluster 7.2 on CentOS 5 (include MySQL 5.5)
  10. IOS程序创建view
  11. MySQL查询(进阶)(每个标点都是重点)
  12. 使用Kubernetes演示金丝雀发布
  13. Scrapy基础(十四)————知乎模拟登陆
  14. AngularJS资源合集[备忘]【申明:来源于网络】
  15. python制作模块
  16. 10.21CRM项目(01)
  17. js 获取当前页url网址信息
  18. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
  19. 循序渐进PYTHON3(十三) --8-- DJANGO之ADMIN
  20. PHP投票实现24小时间隔投票

热门文章

  1. @locked_cached_property ---flask.helpers模块
  2. CD(01背包)
  3. 机器学习基础-Logistic回归1
  4. 2014 蓝桥杯 预赛 c/c++ 本科B组 第九题:地宫取宝(12&#39;) [ dp ]
  5. SOJ 4482 忽悠大神【最小生成树】
  6. OSGI是什么
  7. 重装JDK后Tomcat和Eclipse的配置
  8. JavaOne Online Hands-on Labs
  9. 多平台密码绕过及提权工具Kon-Boot的使用与防范
  10. ADO.NET(OleDb)读取Excel表格时的一个BUG