Codeforces Round #678 (Div. 2)

Codeforces Round #678 (Div. 2) 传送门


A - Reorder

For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑ni=1∑nj=iajj equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 52=2.5.

Input

The first line contains a single integer t — the number of test cases (1≤t≤100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1≤n≤100, 0≤m≤106). The second line contains integers a1,a2,…,an (0≤ai≤106) — the elements of the array.

Output

For each test case print “YES”, if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and “NO” otherwise.

Sample input

2
3 8
2 5 1
4 4
0 1 2 3

Sample output

YES
NO

Note

In the first test case one of the reorders could be [1,2,5]. The sum is equal to (11+22+53)+(22+53)+(53)=8. The brackets denote the inner sum ∑nj=iajj, while the summation of brackets corresponds to the sum over i.

Code

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn= 1e6+6;
const int mod = 1e9+7;
 
ll gcd(ll a ,ll b)
{
	return b == 0? a:gcd(b , a%b);
}
//vector<node>vec[28];
int v[maxn];
int cur = 0;
 
int main()
{
	
	int t;
	cin>>t;
	while(t--)
	{
		int sum = 0;
		int n,m;
		cin>>n>>m;
		for(int i = 0 ; i < n ; i++)
		{
			cin>>v[i];
			sum += v[i];
		}
		if(sum == m)
		{
			cout<<"YES"<<endl;
		}else
		{
			cout<<"NO"<<endl;
		}
	} 
	return 0;
}

B - Prime Square

Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square.
A square of size n×n is called prime if the following three conditions are held simultaneously:
all numbers on the square are non-negative integers not exceeding 105;
there are no prime numbers in the square;
sums of integers in each row and each column are prime numbers.
Sasha has an integer n. He asks you to find any prime square of size n×n. Sasha is absolutely sure such squares exist, so just help him!

Input

The first line contains a single integer t (1≤t≤10) — the number of test cases.
Each of the next t lines contains a single integer n (2≤n≤100) — the required size of a square.

Output

For each test case print n lines, each containing n integers — the prime square you built. If there are multiple answers, print any.

Sample

Sample input

2
4
2

Sample output

4 6 8 1
4 9 9 9
4 10 10 65
1 4 4 4
1 1
1 1

分析

矩阵中每一个数字都不能是素数
行的sum以及列的sum都要为素数
先把 n * n 的矩阵初始化成 1 矩阵,此时每一行和每一列的和均为 n
我们只需要改变主对角线上的元素,就可以使每一行每一列的和都是素数
假设每一行(列)都挖去一个数字 x,则 sum = n - 1
挖去的 x 不上去后要满足行(列)之和为素数,所以x = 素数(P)- sum
求出 x 后把主对角线上的 1 替换成 x 就好啦

Code

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn= 1e6+6;
const int mod = 1e9+7;
 
bool isPrime[maxn];
void sieve()
{
	for(int i = 0 ; i < maxn ; i++)
	{
		isPrime[i] = true;
	}
	for(int i = 2 ; (ll)i*i < maxn ; i++)
	{
		if(isPrime[i])
		{
			for(int j  = 2*i ; j < maxn ; j+=i)
			{
				isPrime[j] =false;
			}
		}
		
	}
}
int vec[128][128];
int main()
{
	sieve();
	int t;
	cin>>t;
	while(t--)
	{
	//	memset(vec,1,sizeof(vec));
		for(int i = 0 ; i < 128 ; i++)
		{
			for(int j = 0 ; j < 128 ; j++)
			{
				vec[i][j] = 1;
			}
		}
		int n ;
		cin>>n;
	// x+sum = p
		int p = n; 
	//	int sum = n-1;
	//	int x = p-sum;
		while(!(isPrime[p] && !isPrime[p-(n-1)]))
		{
			p++;
		}
		for(int i = 0 ; i < n ; i++)
		{
			for(int j = 0 ; j < n  ;j++)
			{
				if(i == j)
				{
					vec[i][j] = p-(n-1);
				}
			}
		}
		//ans
		for(int i = 0  ; i < n ; i++)
		{
			for(int j = 0 ; j < n ; j++)
			{
				cout<<vec[i][j]<<" ";
			}
			cout<<endl;
		}
//		cout<<endl;
	}
	return 0;
}
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页