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 ;
}

SPOJ:Just One Swap(统计&思维)的更多相关文章

  1. SPOJ - COT2 离线路径统计

    题意:求\(u\)到\(v\)的最短路径的不同权值种类个数 树上莫队试水题,这一篇是上篇的弱化部分,但可测试以下结论的正确性 设\(S(u,v)\):\(u-v\)最短路径所覆盖的点集 \(S(u,v ...

  2. [spoj DISUBSTR]后缀数组统计不同子串个数

    题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...

  3. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  4. CentOS 6.5中安装使用dstat资源统计工具

    目录 1 dstat工具的使用 1.1 什么是dstat 1.2 dstat的基本使用 1.3 检测界面各参数的含义 1.4 dstat 的高级用法 2 dstat工具的安装 2.1 (推荐)通过yu ...

  5. Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建

    很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...

  6. 零基础数据分析与挖掘R语言实战课程(R语言)

    随着大数据在各行业的落地生根和蓬勃发展,能从数据中挖金子的数据分析人员越来越宝贝,于是很多的程序员都想转行到数据分析, 挖掘技术哪家强?当然是R语言了,R语言的火热程度,从TIOBE上编程语言排名情况 ...

  7. 进程管理工具htop/glances/dstat的使用

    进程管理工具htop/glances/dstat的使用 Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID). 2.进程的分类 1)根据 ...

  8. 学习ASP.NET Core Razor 编程系列六——数据库初始化

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  9. 使用 ASP.NET Core MVC 创建 Web API(二)

    使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...

随机推荐

  1. Flask设计带认证token的RESTful API接口[翻译]

    上一篇文章, 使用python的Flask实现一个RESTful API服务器端  简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...

  2. Food Delivery (区间DP)

    When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...

  3. jsonp跨域请求实现示例

    网上看了很多关于jsonp的资料,发现在本机运行后实现不了,有的是有错漏,有的是说的比较含糊,接合自己的情况,整了一个可运行的示例: 前言: ajax请求地址:http://192.168.1.102 ...

  4. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  5. 全局Session-GlobalSession

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.spri ...

  6. HTTP错误:java.lang.IllegalArgumentException: Illegal character in scheme at index 0: http://xxxxxx

    读取T卡文件里的域名,HTTP请求出现如下错误 java.lang.IllegalArgumentException: Illegal character in scheme at index 0: ...

  7. Codevs 队列练习 合并版

    3185 队列练习 1  时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这 ...

  8. 51nod1135 原根

    原根判定:$m>2$,$\varphi (m)$的不同素数是$q_1,q_2,……,q_s$,$(g,m)=1$,则$g$是$m$的一个原根的充要条件是$g^{\frac{\varphi(m)} ...

  9. x1 carbon 扩展屏 模糊

    x1 carbon 扩展屏 模糊,扩展屏是dell的屏,分辨率最大是1920*1080, x1最大是2560*1440. 不论是通过DP mini转VGA,还是HDMI,输出都是模糊,只有复制屏幕的时 ...

  10. Animation显示ListView的每一条记录

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...