欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GYM-2014-2015 Andrew Stankevich Contest 47 (ASC 47) E Elegant Square(构造)

程序员文章站 2022-03-19 13:45:07
...

GYM-2014-2015 Andrew Stankevich Contest 47 (ASC 47) E Elegant Square(构造)

题意:要求构造一个N*N的矩阵,使得所有数字都不相同且每一行每一列的乘积相同,且每个数字都不含平方因子

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=a-1;i>=(b);--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define clr(a,b) memset(a,b,sizeof(a))
#define eps 1e-10
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef unsigned int ui;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9 + 7;
const int MX = 1e3 + 5;

int a[MX], p[MX], sz;
void init() {
    rep(i, 2, MX) if (!a[i]) {
        p[sz++] = i;
        for (int j = i + i; j < MX; j += i) a[j] = 1;
    }
}

ll mp[35][35][35];
int c[35], r[35], cr[35][35];
map<ll, int>vis;

void update (ll& x, int p, int& c, int& r, int& cr) {
    x *= p;
    c++, r++, cr++;
    vis[x]++;
}
void print (int n) {
    rep(i, 0, n) rep(j, 0, n) printf ("%lld%c", mp[n][i][j], j == n - 1 ? '\n' : ' ');
}
void solve (int n) {
    rep(i, 0, n) rep(j, 0, n) mp[n][i][j] = 1, cr[i][j] = 0;
    vis.clear();
    for (int cnt = n, num = 1, u = 0; cnt; cnt /= 2, num++) {
        clr(c, 0), clr(r, 0);
        for (int i = 0; i < n; i++, u++) {
            for (int t = 0, j = i, k = 0; t < n; t++, j = (j + 1) % n) {
                while (c[j] != i || r[k] != i || cr[j][k] == num || vis[mp[n][j][k]*p[u]] == cnt) {
                    k = (k + 1) % n;
                }
                update (mp[n][j][k], p[u], c[j], r[k], cr[j][k]);
            }
        }
    }
}

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    freopen("elegant.in", "r", stdin);
    freopen("elegant.out", "w+", stdout);
    init();
    rep(n, 3, 31) solve (n);
    int n;
    while (~scanf ("%d", &n)) print (n);
    return 0;
}

 

相关标签: 构造