Codeforces Round 871 (Div. 4)

478 阅读2分钟

F

A snowflake graph is generated from two integers x and y, both greater than 1, as follows:

  • Start with one central vertex.
  • Connect x new vertices to this central vertex.
  • Connect y new vertices to each of these x vertices.

For example, below is a snowflake graph for x=5 and y=3.

The snowflake graph above has a central vertex 15, then x=5 vertices attached to it (3, 6, 7, 8, and 20), and then y=3 vertices attached to each of those.

Given a snowflake graph, determine the values of x and y.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains two integers n and m (2≤n≤200; 1≤m≤min(1000,n(n−1)2) — the number of vertices and edges in the graph, respectively.

The next m lines each contain two integers each u and v (1≤u,v≤n) — the numbers of vertices connected by an edge. The graph does not contain multiple edges and self-loops.

It is guaranteed that this graph is a snowflake graph for some integers x and y both greater than 1.

Output

For each test case, on a separate line output the values of x and y, in that order, separated by a space.

Example

input

Copy

3
21 20
21 20
5 20
13 20
1 3
11 3
10 3
4 8
19 8
14 8
9 7
12 7
17 7
18 6
16 6
2 6
6 15
7 15
8 15
20 15
3 15
7 6
1 2
1 3
2 4
2 5
3 6
3 7
9 8
9 3
3 6
6 2
2 1
5 2
2 7
4 3
3 8

output

Copy

5 3
2 2
2 3

Note

The first test case is pictured in the statement. Note that the output 3 5 is incorrect, since x should be output before y.

思路:直接先随便拿一个点当根找出和他最远的点f再拿f找出离f最远的点那么这两点就是最长的链,他们的中点就是根,在求出根的子树有多少个,和他一个子树的叶子节点有多少个就是答案了

代码

#include <bits/stdc++.h>

using namespace std;
#define int long long 
#define sc(x) scanf("%lld",&x)
const int N = 1e3 + 10;
int n,m;
int f[N],fa[N];

vector<int>e[N];
void dfs(int u,int v)
{
	f[u] = f[v] + 1;
	fa[u] = v;
	for(auto c:e[u])
	{
		if(c==v)
		continue;
		dfs(c,u);
	}
}
void solve()
{
	cin >> n >> m;
	for(int i = 0;i <= n;i ++)
	f[i] = 0,e[i].clear();
	
	for(int i = 1;i <= m;i ++)
	{
		int x,y;
		cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(1,0);
	int w = 0,u = 0;
	f[0] = 0;
	for(int i = 1;i <= n;i ++)
	{
		if(f[i] > u)
		{
			w = i;
			u = f[i];
		}
		f[i] = 0;
	}
	dfs(w,0);
	//cout << w << '\n';
	int ans = 0;
	int mx = 0;
	for(int i = 1;i <= n;i ++)
	{
		mx = max(mx,f[i]);
		//cout << f[i] << " \n"[i == n];
	}
	for(int i = 1;i <= n;i ++)
	{
		if(f[i] == (mx+1)/2)
		{
			u = e[i].size();
			w = e[e[i][0]].size()-1;
			if(u*w+u==m)
			{
				cout << u << ' ' << w << '\n';
				return ;
			} 
		}
	}
}
signed main()
{
	int tt = 1;
	sc(tt);
	while(tt--)
	{
		solve();
	}
}

G

In a carnival game, there is a huge pyramid of cans with 2023 rows, numbered in a regular pattern as shown.

If can 9292 is hit initially, then all cans colored red in the picture above would fall.

You throw a ball at the pyramid, and it hits a single can with number n2. This causes all cans that are stacked on top of this can to fall (that is, can n2 falls, then the cans directly above n2 fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can 92 is hit.

What is the sum of the numbers on all cans that fall? Recall that n2=n×n.

Input

The first line contains an integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains a single integer n (1≤n≤106) — it means that the can you hit has label n2.

Output

For each test case, output a single integer — the sum of the numbers on all cans that fall.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). For all valid inputs, the answer will always fit into 64-bit integer type.

Example

input

Copy

10
9
1
2
3
4
5
6
10
1434
1000000

output

Copy

156
1
5
10
21
39
46
146
63145186
58116199242129511

Note

The first test case is pictured in the statement. The sum of the numbers that fall is

12+22+32+52+62+92=1+4+9+25+36+81=156.

In the second test case, only the can labeled 1212 falls, so the answer is 12=1.

In the third test case, the cans labeled 1212 and 2222 fall, so the answer is 12+22=1+4=5.

In the fourth test case, the cans labeled 1212 and 3232 fall, so the answer is 12+32=1+9=10.

In the fifth test case, the cans labeled 1212, 2222, and 4242 fall, so the answer is 12+22+42=1+4+16=21.

思路:其实就是一个线性dp,从上面的数转移到下面的数,别忘记要减去上面的数的数

代码:


#include <bits/stdc++.h>

using namespace std;
#define int long long 
const int N = 3e6 + 10;
int n,m;
int e[N];
map<pair<int,int> ,int >mp;
void solve()
{
	cin >> n;
	cout << e[n] << '\n';
}
signed main()
{
	int tt = 1;
	int l = 1,r = 1;
	e[l] = 1;
	l = 2,r = 1;
	for(int i = 1;i <= 2000;i ++)
	{
		for(int j = 0;j < i;j ++)
		{
			mp[{l,l+1}] = r + j; 
			l ++;
		}
		l ++;
		r += i;
		//cout << r << ' ' << l << '\n'; 
	}
	//cout << mp[{8,9}] << '\n';
	l = 1,r = 1;
	for(int i = 2;i <= 2000;i ++)
	{
		for(int j = 1;j <= i;j ++)
		{
			if(j == 1||j == i)
			{
				e[r+j] += e[l];
			}
			else
			{
				e[r+j] += e[l] ;
				e[r+j] += e[l+1];
				e[r+j] -= e[mp[{l,l+1}]];
				l++;
			}
			e[r+j] += (r+j)*(r+j);
		}
		l = r+1;
		r = r+i;
	}
	sc(tt);
	while(tt--)
	{
		solve();
	}
}