文章目录

  • A. Pretty Permutations
  • B. Pleasant Pairs
  • C. Great Graphs

A. Pretty Permutations

There are nnn cats in a line, labeled from 111 to nnn, with the iii-th cat at position iii. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.

For example, if there are 333 cats, this is a valid reordering: [3,1,2][3, 1, 2][3,1,2]. No cat is in its original position. The total distance the cats move is 1+1+2=41 + 1 + 2 = 41+1+2=4 as cat 111 moves one place to the right, cat 222 moves one place to the right, and cat 333 moves two places to the left.

Input

The first line contains a single integer ttt (1≤t≤1001 \leq t \leq 1001≤t≤100) — the number of test cases. Then ttt test cases follow.

The first and only line of each test case contains one integer nnn (2≤n≤1002 \leq n \leq 1002≤n≤100) — the number of cats.

It can be proven that under the constraints of the problem, an answer always exist.

Output

Output ttt answers, one for each test case. Each answer consists of nnn integers — a permutation with the minimum total distance. If there are multiple answers, print any.

Example

input

2
2
3

output

2 1
3 1 2

Note

For the first test case, there is only one possible permutation that satisfies the conditions: [2,1][2, 1][2,1].

The second test case was described in the statement. Another possible answer is [2,3,1][2, 3, 1][2,3,1].

#include <iostream>
#include <algorithm>
using namespace std;int main(){int t;cin >> t;while(t--){int n;cin >> n;if(n%2==0){for(int i=1; i<n; ){cout << i+1 <<" " << i <<" ";i += 2;}cout << endl;}else{for(int i=1; i<=n-3; ){cout << i+1 <<" " << i <<" ";i += 2;}cout << n << " " << n-2 << " " << n-1 << " " ;cout << endl;}}return 0;
}

B. Pleasant Pairs

You are given an array a1,a2,…,ana_1, a_2, \dots, a_na1​,a2​,…,an​ consisting of nnn distinct integers. Count the number of pairs of indices (i,j)(i, j)(i,j) such that i<ji < ji<j and ai⋅aj=i+ja_i \cdot a_j = i + jai​⋅aj​=i+j.

Input

The first line contains one integer ttt (1≤t≤1041 \leq t \leq 10^41≤t≤104) — the number of test cases. Then ttt cases follow.

The first line of each test case contains one integer nnn (2≤n≤1052 \leq n \leq 10^52≤n≤105) — the length of array aaa.

The second line of each test case contains nnn space separated integers a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ (1≤ai≤2⋅n1 \leq a_i \leq 2 \cdot n1≤ai​≤2⋅n) — the array aaa. It is guaranteed that all elements are distinct.

It is guaranteed that the sum of nnn over all test cases does not exceed 2⋅1052 \cdot 10^52⋅105.

Output

For each test case, output the number of pairs of indices (i,j)(i, j)(i,j) such that i<ji < ji<j and ai⋅aj=i+ja_i \cdot a_j = i + jai​⋅aj​=i+j.

Example

input

3
2
3 1
3
6 1 5
5
3 1 5 9 2

output

1
1
3

Note

For the first test case, the only pair that satisfies the constraints is (1,2)(1, 2)(1,2), as a1⋅a2=1+2=3a_1 \cdot a_2 = 1 + 2 = 3a1​⋅a2​=1+2=3

For the second test case, the only pair that satisfies the constraints is (2,3)(2, 3)(2,3).

For the third test case, the pairs that satisfy the constraints are (1,2)(1, 2)(1,2), (1,5)(1, 5)(1,5), and (2,3)(2, 3)(2,3).

分析

a[i] * a[j] ,根据 a[i] 来找 a[j] , i + j 一定是 a[i] 的倍数,i 是不变的,所以 j 的增加量一定是a[i] 的倍数。

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll a[N];int main(){int T;cin >> T;while(T--){int n;cin >> n;ll ans = 0;for(int i = 1; i <= n; i++)cin >> a[i];for(int i = 1; i <= n; i++)for(int j = a[i] - i; j <= n; j += a[i]){if(j <= i)continue;if(a[i] * a[j] == i + j)ans++;}cout << ans << endl;}return 0;}

C. Great Graphs

Farmer John has a farm that consists of nnn pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.

Unfortunately, Farmer John lost the map of the farm. All he remembers is an array ddd, where did_idi​ is the smallest amount of time it took the cows to reach the iii-th pasture from pasture 111 using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.

Input

The first line contains one integer ttt (1≤t≤1041 \leq t \leq 10^41≤t≤104) — the number of test cases. Then ttt cases follow.

The first line of each test case contains a single integer nnn (1≤n≤1051 \leq n \leq 10^51≤n≤105) — the number of pastures.

The second line of each test case contains nnn space separated integers d1,d2,…,dnd_1, d_2, \ldots, d_nd1​,d2​,…,dn​ (0≤di≤1090 \leq d_i \leq 10^90≤di​≤109) — the array ddd. It is guaranteed that d1=0d_1 = 0d1​=0.

It is guaranteed that the sum of nnn over all test cases does not exceed 10510^5105.

Output

For each test case, output the minimum possible cost of a farm that is consistent with Farmer John’s memory.

Example

input

3
3
0 2 3
2
0 1000000000
1
0

output

-3
0
0

Note

In the first test case, you can add roads

  • from pasture 111 to pasture 222 with a time of 222,
  • from pasture 222 to pasture 333 with a time of 111,
  • from pasture 333 to pasture 111 with a time of −3-3−3,
  • from pasture 333 to pasture 222 with a time of −1-1−1,
  • from pasture 222 to pasture 111 with a time of −2-2−2.

The total cost is 2+1+−3+−1+−2=−32 + 1 + -3 + -1 + -2 = -32+1+−3+−1+−2=−3.

In the second test case, you can add a road from pasture 111 to pasture 222 with cost 100000000010000000001000000000 and a road from pasture 222 to pasture 111 with cost −1000000000-1000000000−1000000000. The total cost is 1000000000+−1000000000=01000000000 + -1000000000 = 01000000000+−1000000000=0.

In the third test case, you can’t add any roads. The total cost is 000.

分析

相当于负向的边全部加上,正向只在相邻pasture加边

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;const int N = 1E5 + 10;
int a[N];
ll sum,ans;int main(){int T;cin >> T;while(T--){int n;cin >> n;sum = ans = 0;for(int i=0; i<n; i++){cin >> a[i];sum += a[i];}sort(a, a+n); // 由小到大排序 ans = sum;for(int i=1; i<n-1; i++){sum -= 1ll*(n-i)*(a[i]-a[i-1]);ans += sum;}ans -= a[n-1];cout << -ans <<endl; }return 0;
}

Codeforces Round #728 (Div. 2)相关推荐

  1. Codeforces Round #632 (Div. 2) E. Road to 1600 构造好题

    传送门 文章目录 题意: 思路 题意: 直接白嫖 思路 首先不难发现,n≤2n\le2n≤2的时候是无解的. 现在我们来构造n=3n=3n=3的情况,通过打表可以发现如下矩阵是符合题目要求的: 179 ...

  2. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  3. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  4. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  5. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  6. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  7. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  8. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  9. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

最新文章

  1. java stream 多个filter_恕我直言你可能真的不会java第3篇:Stream的Filter与谓词逻辑...
  2. python编程神器下载_Python编程神器 -程序员必备开发手册
  3. LiveVideoStackCon讲师热身分享 ( 十二 ) —— 微博短视频高并发架构
  4. 2021年量子计算机奖,中兴通讯携手中国移动共获“2021年未来网络领先创新科技成果”奖...
  5. 4月第三周国内域名解析商Top10:万网升至20.32%
  6. oracle in和exist的区别 not in 和not exist的区别
  7. VC2012 小助手的破解
  8. [TIPTOP] 鼎捷ERP開發小撇步 - 編譯程式碼 及 畫面檔 一次到位的方法
  9. 3dmax模型导入unity
  10. POJO、PO、DTO、DAO、BO、VO需要搞清楚的概念
  11. 智能音箱---TAS5754M 音频DSP 到Android
  12. java求三角形的面积_java编程中求三角形面积怎么写?
  13. DBeaver打开sql文件中文乱码问题解决
  14. 费马小定理 (证明)
  15. 深圳首辆数字人民币主题观光巴士亮相
  16. Excel常用的操作
  17. 【DP】AGC012 E Camel and Oases
  18. Linux自动巡检脚本
  19. 欢迎 收听 海口DJ江林 混音 你的样子 异域风格 为您倾心打造 DJ 江林 reim 短指键盘松哥原创
  20. HTML实现狗屁不通文章生成器

热门文章

  1. 第三回 Bootstrap3.x 起步
  2. Debug Android with Android phone.
  3. 《编程导论(Java)·9.3.1回调·3》回调的实现
  4. 实例方法、静态方法和类方法的区别
  5. LeetCode(62):不同路径
  6. 一个 Java 的 Socket 服务器和客户端通信的例子
  7. [Hive] - Hive参数含义详解
  8. [转]各种互斥量的总结
  9. Andriod Studio 使用心得,持续更新中
  10. 微信支付开发(2) 扫码支付模式一