POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{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枚举)的更多相关文章
- Flip Game (高斯消元 || dfs)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- BZOJ1770:[USACO]lights 燈(高斯消元,DFS)
Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...
- POJ 1681 Painter's Problem 【高斯消元 二进制枚举】
任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total ...
- [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)
传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...
- Codeforces 1163E 高斯消元 + dfs
题意:给你一个集合,让你构造一个长度尽量长的排列,使得排列中任意相邻两个位置的数XOR后是集合中的数. 思路:我们考虑枚举i, 然后判断集合中所有小于1 << i的数是否可以构成一组异或空 ...
- bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】
参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...
- [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...
- [HIHO1196]高斯消元·二(高斯消元、枚举自由变元)
题目链接:http://hihocoder.com/problemset/problem/1196 #include <bits/stdc++.h> using namespace std ...
- POJ 3185 The Water Bowls 【一维开关问题 高斯消元】
任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- Android进程保活
Android进程回收机制 Low Memory Killer原理 微信团队原创分享:Android版微信后台保活实战分享(网络保活篇) 微信团队原创分享:Android版微信后台保活实战分享(进程保 ...
- 教你一招:win 7 或win 10右键菜单 添加获取管理员权限
当我们从经典的 windows XP 系统升级到 win 7 或 win 10,我们会发现,想要删除一些文件时,总是提示被占用或者是没有权限,很是烦恼. 这里,写下解决这个烦恼的办法,以安慰心里的不平 ...
- BZOJ4533 [BeiJing2014 WinterCamp] 数据
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- please wait while windows configures microsoft visual studio professional 2013 [转载]
2016年5月30日 VS3013安装update 2以后,打开offie出现"please wait while windows configures microsoft visual s ...
- FMDB 数据库
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- 11月8日PHP练习《留言板》
一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php 登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- python 面向对象(类)
面向对象,即是将具备某种共性的事物抽象成一个类(模板),然后再根据类来创建实例对象进行具体的使用. 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写, ...
- js 闭包 理解
1.什么是闭包 定义:是指有权访问另一个函数作用域中的变量的函数 创建闭包:在一个函数内部创建另一个函数 基本特点 在返回的匿名函数中 可以调用外部函数的变量 如下例中所示 内部函数(匿名函数) 可以 ...
- Python之路【第二十一篇】Django ORM详解
ORM回顾 关系对象映射(Object Relational Mapping,简称ORM). django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表. 对于ORM框 ...
- There are no resources that can be added or removed from the server
第1步.新建一个“Dynamic Web Project” 第2步.把新建项目里面的.project文件和.settings文件夹复制到导入的那个项目里面. 第3步.把web projiect set ...