Levko loves tables that consist of n rows and n columns very much. He especially loves beautiful tables. A table is beautiful to Levko if the sum of elements in each row and column of the table equals k.
Unfortunately, he doesn't know any such table. Your task is to help him to find at least one of them.
The single line contains two integers, n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000).
Print any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value.
If there are multiple suitable tables, you are allowed to print any of them.
2 4
1 3 3 1
4 7
2 1 0 4 4 0 2 1 1 3 3 0 0 3 2 2
In the first sample the sum in the first row is 1 + 3 = 4, in the second row — 3 + 1 = 4, in the first column — 1 + 3 = 4 and in the second column — 3 + 1 = 4. There are other beautiful tables for this sample.
In the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables that meet the statement requirements.
题意是给你n和k,让你构造一个n*n的矩阵使得该矩阵的行之和,列之和均为k。
直接YY即可。
/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 30 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);
typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
int n , k;
while(~scanf("%d%d" , &n , &k))
{
for(int i = 0; i < n ; i++)
{
for(int j = 0 ;j < n ; j++)
{
if(i == j)printf("%d%c" , k , j == n - 1 ? '\n' : ' ');
else printf("0%c" , j == n - 1 ? '\n' : ' ');
}
}
}
return 0;
}
———————————————————————————————————————————————————
***********************************************************************************************************************************———————————————————————————————————————————————————
Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.
Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.
The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n).
In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist.
If there are multiple suitable permutations, you are allowed to print any of them.
4 2
2 4 3 1
1 1
-1
In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.
The second sample has no beautiful permutations.
思路:我们可以肯定相邻两数必然互素,1与任何数互素,那么我们就把k提到首位然后按顺序输出即可。
/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 30 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);
typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[101100];
int main()
{
int n , k;
while(~scanf("%d%d" , &n , &k))
{
for(int i = 0; i< n ; i++)a[i] = i + 1;
if(k == n)
{
puts("-1");
continue;
}
printf("%d" , a[n - k - 1]);
for(int i = 0 ; i < n - k - 1 ; i++)
{
printf(" %d" , a[i]);
}
for(int i = n - k ;i < n ; i++)
{
printf(" %d" , a[i]);
}
printf("\n");
}
return 0;
}
———————————————————————————————————————————————————***********************************************************************************************************************************———————————————————————————————————————————————————
思路:记录每个点的add,表示加了多少,记录每个点的ub,表示每个点可行的上界,然后暴力去扫即可,最后向回扫一遍,判断是否成立。
/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 5050 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);
typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int ub[MAXN] , n , m , add[MAXN] , Q[MAXN][4];
int main()
{
int x , l , r , k;
while(~scanf("%d%d" , &n , &m))
{
for(int i = 1 ; i <= n ; i++)
{
add[i] = 0;
ub[i] = 1000000000;
}
clr(Q , 0);
for(int i = 0 ; i < m ; i++)
{
scanf("%d%d%d%d", &x , &l , &r , &k);
Q[i][0] = x , Q[i][1] = l , Q[i][2] = r , Q[i][3] = k;
if(x == 1)
{
for(int j = l ; j <= r ; j++)
{
add[j] += k;
}
}
else
{
for(int j = l ; j <=r ; j++)
{
ub[j] = min(ub[j] , k - add[j]);
}
}
}
// for(int i = 1; i <= n ; i++)cout << ub[i] << endl;
bool fg = 0;
for(int i = 1 ; i <= n ; i++)add[i] = 0;
for(int i = 0 ; i < m ; i++)
{
l = Q[i][1] , r = Q[i][2] , k = Q[i][3];
if(Q[i][0] == 1)
{
for(int j = l ; j <= r ; j++)add[j] += k;
}
else
{
int j;
for(j = l ;j <= r ; j++)if(ub[j] == k - add[j])break;
// cout << j << endl;
if(j > r)
{
fg = 1;
break;
}
}
}
printf("%s\n" , fg ? "NO" : "YES");
if(!fg)
{
for(int i = 1 ; i <= n ; i++)
{
printf("%d%c" , ub[i] , i == n ? '\n' : ' ');
}
}
}
return 0;
}
———————————————————————————————————————————————————***********************************************************************************************************************************———————————————————————————————————————————————————
思路是二分。
/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 2000 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);
typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , b[MAXN] , sum , k ,n;
bool ok(int d)
{
for(int i = 1 ; i<= n; i++)
{
b[i] = 1;
}
for(int i = 2 ; i <= n ; i++)
{
for(int j = 1 ; j < i ; j++)
{
if(abs(a[i] - a[j]) <= (LL)(i - j) * d)
{
b[i] = max(b[i] , b[j] + 1);
}
}
}
sum = 0;
for(int i = 1 ; i <= n ;i++)sum = max(sum , b[i]);
return n - sum <= k;
}
int main()
{
int l = 0 , r = 2*1000000000;
scanf("%d%d" , &n , &k);
for(int i = 1 ; i <= n ; i++)scanf("%d" , &a[i]);
while(l <= r)
{
int Mid = ((LL)l + r) / 2;
if(ok(Mid))r = Mid - 1;else l = Mid + 1;
}
printf("%d\n" , l);
return 0;
}