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;
}
相關文章
- Codeforces Round #541 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round 987 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 986 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 967 (Div. 2)
- Codeforces Round 965 (Div. 2)
- Codeforces Round 963 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 945 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 932 (Div. 2) ABCD
- Codeforces Round 945 (Div. 2) (A - E)
- Codeforces Round 892 (Div. 2) E
- Codeforces Round 936 (Div. 2) E
- 2024.11.15 Codeforces Round 987(Div. 2)