由于解集只为{0, 1, 2}故消元后需dfs枚举求解

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 60, INF = 0x3F3F3F3F; int a[N][N], mod;
int x[N];
int n, row;
int ans[N];
bool ok; int gauss(int a[][N], int n){
int i, j;
for(i = 0, j = 0; i < n && j < n; i++, j++){
int r = i;
for(int k = i; k < n; k++){
if(a[k][j]){
r = k;
break;
}
}
if(a[r][j] == 0){
i--;
continue;
}
if(r != i){
for(int k = 0; k <= n; k++){
swap(a[i][k], a[r][k]);
}
}
for(int k = i + 1; k < n; k++){
if(a[k][j]){
int x1 = a[i][j], x2 = a[k][j];
for(int l = j; l <= n; l++){
a[k][l] = (a[k][l] * x1 - x2 * a[i][l]) % mod;
}
}
}
}
return i;
} void dfs(int r){
if(r == -1){
ok = 1;
return;
}
if(ok){
return;
}
int x = 0;
while(x < n && a[r][x] == 0){
x++;
}
if(x == n){
if(a[r][n]){
return;
}
for(ans[x] = 0; ans[x] <= 2; ans[x]++){
dfs(r - 1);
}
return;
}
int tp = 0;
for(int j = x + 1; j < n; j++){
tp += a[r][j] * ans[j];
tp %= mod;
}
for(ans[x] = 0; ans[x] <= 2; ans[x]++){
if((ans[x] * a[r][x] + tp - a[r][n]) % mod == 0){
dfs(r - 1);
}
}
} int main(){
int t;
cin>>t;
while(t--){
cin >> mod >> n;
for(int i = 0; i < n; i++){
cin >> x[i];
}
for(int i = 0; i < n; i++){
a[i][n] = (i == 0);
for(int j = 0, k = i; j <= i; j++ , k--){
a[i][j] = x[k];
}
for(int j = i + 1, k = n -1; j < n; j++, k--){
a[i][j] = x[k];
}
}
row = gauss(a, n);
ok = 0;
dfs(n - 1);
if(ok){
printf("A solution can be found\n");
}else{
printf("No solution\n");
} } return 0;
}

  

POJ1288 Sly Number(高斯消元 dfs枚举)的更多相关文章

  1. Flip Game (高斯消元 || dfs)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  2. BZOJ1770:[USACO]lights 燈(高斯消元,DFS)

    Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...

  3. POJ 1681 Painter's Problem 【高斯消元 二进制枚举】

    任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total ...

  4. [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)

    传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...

  5. Codeforces 1163E 高斯消元 + dfs

    题意:给你一个集合,让你构造一个长度尽量长的排列,使得排列中任意相邻两个位置的数XOR后是集合中的数. 思路:我们考虑枚举i, 然后判断集合中所有小于1 << i的数是否可以构成一组异或空 ...

  6. bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】

    参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...

  7. [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

    题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...

  8. [HIHO1196]高斯消元·二(高斯消元、枚举自由变元)

    题目链接:http://hihocoder.com/problemset/problem/1196 #include <bits/stdc++.h> using namespace std ...

  9. POJ 3185 The Water Bowls 【一维开关问题 高斯消元】

    任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. Oracle之分页查询

    select * from ( select a.*, rownum rn from (select * from table_name) a where rownum<= 40 ) where ...

  2. python 批量扫描mongodb 未授权访问脚本

    需要 pymongo库easy_install pymongo脚本: import socket import sys import pymongo ipcons = [] def Scanner(i ...

  3. Java细粒度锁实现的3种方式

    最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性,并且要求加锁后性能不能受到太大的影响.初步的想法是通过数据的时间戳,id等关键字来加锁,从而保证不同类型数据处理的并发性.而java自 ...

  4. C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

    1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...

  5. 双守护进程(不死service)-5.0系统以下

    上链接: http://files.cnblogs.com/files/andlp/DaemonProcess.zip 5.0以上  参考marsDaemon

  6. vtkMapper

    本文只是整理了该网页的内容:http://www.cnblogs.com/lizhengjin/archive/2009/08/16/1547340.html vtkMapper是一个抽象类,指定了几 ...

  7. WindowsPhone8.1RT建立空白应用挂起没反应的解决方案

    wp8.1下, 建立空白应用后遇到的问题:想要实现保存.提取数据都无法成功 在退出程序后无法进入到OnNavigatedFrom()中去. 网上说要手动的Invoke OnSuspending事件. ...

  8. [Centos] Centos 7笔记

    命令行与图形界面 在X-Window图形操作界面中按“Alt+Ctrl+功能键Fnn=1~6”就可以进入Console字符操作界面.这就意味着你可以同时拥有X-Window加上6个Console字 ...

  9. R笔记 单样本t检验 功效分析

    R data analysis examples 功效分析 power analysis for one-sample t-test单样本t检验 例1.一批电灯泡,标准寿命850小时,标准偏差50,4 ...

  10. 如何删除PHP数组中的元素,并且索引重排(unset,array_splice)?

    如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset($arr[1]); pri ...