51nod 1323 完美平方

本文介绍了一种解决异或方程组的方法——高斯消元法,并通过实例详细展示了如何设置未知数、建立方程以及计算自由元的过程。此外,还提到了一种优化手段bitset的应用。

题目链接.

异或方程组高斯消元例题。

每个元素选与不选设为未知数。

同一行同一列搞个方程。

至于是完全平方数就分解质因数,对每个质因子建一个方程。

答案是2的自由元个数次幂。

自由元个数=元的个数-有用方程的条数。

其实我到现在才知道高斯消元非三角矩阵的打法,感觉以前学了假的消元。

可以用bitset优化,很简单,这里不讲。

算法竞赛入门经典这本书里有一道类似的题,打法也是从那里copy的。

Code:

#include<set>
#include<cstdio>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

const int N = 21, mo = 1e9 + 7;
const int M = 1e5;

int T;
int p[M], bz[M];
int n, a[N][N], num[N][N];
int u[N][N][20], v[N][N][20];
int b[N * 100][N * N], tot;
set<int> s;
int ans = 1;

void Build() {
    fo(i, 2, M)  {
        if(!bz[i]) p[++ p[0]] = i;
        fo(j, 1, p[0]) {
            int k = i * p[j];
            if(k > M) break;
            bz[k] = 1;
            if(i % p[j] == 0) break;
        }
    }
}

void Clear() {
    fo(i, 1, tot) fo(j, 0, n * n) b[i][j] = 0;
    tot = 0;
}

void Built() {
    scanf("%d", &n);
    fo(i, 1, n) fo(j, 1, n) num[i][j] = (i - 1) * n + j;
    fo(i, 1, n) {
        b[++ tot][0] = 1;
        fo(j, 1, n) b[tot][num[i][j]] = 1;
    }
    fo(j, 1, n) {
        b[++ tot][0] = 1;
        fo(i, 1, n) b[tot][num[i][j]] = 1;
    }
    fo(i, 1, n) fo(j, 1, n) scanf("%d", &a[i][j]);
    fo(i, 1, n) {
        fo(j, 1, n) {
            int x = a[i][j]; u[i][j][0] = 0;
            for(int k = 1; k <= p[0] && p[k] * p[k] <= x; k ++)
                if(x % p[k] == 0) {
                    u[i][j][++ u[i][j][0]] = p[k];
                    v[i][j][u[i][j][0]] = 0;
                    while(x % p[k] == 0)
                        v[i][j][u[i][j][0]] ++, x /= p[k];
                }
            if(x > 1) u[i][j][++ u[i][j][0]] = x, v[i][j][u[i][j][0]] = 1;
            fo(k, 1, u[i][j][0]) s.insert(u[i][j][k]);
        }
    }
    while(!s.empty()) {
        int x = *s.begin(); s.erase(x);
        b[++ tot][0] = 0;
        fo(i, 1, n) {
            fo(j, 1, n) {
                fo(k, 1, u[i][j][0]) if(u[i][j][k] == x && v[i][j][k] % 2)
                    b[tot][num[i][j]] = 1;
            }
        }
    }
}

void W() {
    fo(i, 1, tot) {
        fo(j, 0, n * n) printf("%d ", b[i][j]);
        printf("\n");
    }
    printf("\n");
}

int Solve(int m, int n) {
    int i = 1, j = 1;
    while(i <= m && j <= n) {
        int r = i;
        fo(k, i, m) if(b[k][j]) r = k;
        if(b[r][j]) {
            fo(k, 0, n) swap(b[i][k], b[r][k]);
            fo(u, i + 1, m) if(b[u][j])
                fo(k, 0, n) b[u][k] ^= b[i][k];
            i ++;   
        }
        j ++;
    }
    return n - i + 1;
}


void pd() {
    fo(i, 1, tot) {
        int bz = 1;
        fo(j, 1, n * n) if(b[i][j]) {
            bz = 0; break;
        }
        if(bz && b[i][0]) {
            ans = 0; return;
        }
    }
}

int main() {
    Build();
    for(scanf("%d", &T); T; T --) {
        Clear();
        Built();
        int r = Solve(tot, n * n);
        ans = 1; fo(i, 1, r) ans = (ans * 2) % mo;
        pd(); printf("%d\n", ans);
    }
}
题目 51nod 3478 涉及一个矩阵问题,要求通过最少的操作次数,使得矩阵中至少有 `RowCount` 行和 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行和列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行和每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行和列组合**: - 由于 `N` 和 `M` 的最大值为 8,因此可以枚举所有可能的行组合和列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行和列需要修改,并且注意行和列的交叉点可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举和位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float('inf') # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行和每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行和列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行和列交叉点的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` 和 `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行和列的枚举组合以减少计算时间? 2. 在计算行和列的交叉点时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行和列的回文条件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值