F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

permu

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 143    Accepted Submission(s): 16


Problem Description
ZZX has a sequence a[1..n], which is a permutation of 1,2,...,n.

Now ZZX wants to perform some modifications on this sequence: every time he can choose a pair of integers i,j, satisfying 1<=i<j<=n and a[i]>a[j], then swap a[i] and a[j].

If a permutation b[1..n] can be obtained by performing some modifications (possibly nothing is performed) on the initial sequence a, then ZZX says b is reachable from a.

Now JRY has m sequences a_1[1..n], a_2[1..n], ..., a_m[1..n]. Each of them is a permutation of 1,2,...,n. He wants to know how many pairs of (i,j) (1<=i<=m,1<=j<=m) satisfy a_i is reachable from a_j.
 

Input
First line contains an integer t. Then t testcases follow.

In each testcase: First line contains two integers n and m. Next m lines, each contains n integers a_i[1],a_i[2],...,a_i[n].

a_i is a permutation of 1,2,...,n.

There are at most 1000 small testcases and 1 big testcase. Small testcases satisfy 1<=n<=5 and 1<=m<=500. Big testcase satisfies 1<=n<=9, 1<=m<=300000.
 

Output
For each testcase print one integer as your answer in a line.
 

Sample Input
2 3 3 1 2 3 3 1 2 2 3 1 2 2 1 2 1 2
 

Sample Output
5 4
 

Author
ѧ¾üÖÐѧ
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 16:44:44, Gzip enabled