打表题。。
规律是找出来了 奈何优化不了 。。。。
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d ", a) #define plld(a) printf("%lld ", a) #define pc(a) printf("%c ", a) #define ps(a) printf("%s ", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1100, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int way[maxn][maxn]; int sta[maxn]; void solve() { way[1][1] = 1; for(int i = 2; i <= 1024; i *= 2) { int k = i >> 1; for(int x = 1; x <= k; x++) for(int y = 1; y <= k; y++) { way[x + k][y + k] = way[x][y]; way[x + k][y] = way[x][y + k] = way[x][y] + k; } } } int main() { int T, n, k; solve(); cin >> T; while(T--) { int cnt = 0; cin >> n >> k; // if((n & 1) || !(n & 1) && k > n - 1) // { // cout << "Impossible" << endl; // continue; // } for (int i = 2; i <= n; i++) { bool flag = 0; for (int j = 1; j <= n; j++) { if(way[i][j] > n) { flag = 1; break; } } if(!flag) sta[cnt++] = i; } if(cnt < k) { cout << "Impossible" << endl; continue; } for(int i = 0; i < k; i++) for(int j = 1; j <= n; j++) printf("%d%s", way[sta[i]][j], (j == n ? " " : " ")); } return 0; }