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 ...
随机推荐
- java-工具类-读取配置文件
java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...
- SSIS同步多个数据库
这周接到了一个新的需求,从IBM DB2,同步数据到SQLServer.在从SQLServer,同步到Oracle. 因为IBM是32位的平台,ORACLE是64位的平台.而且要求使用计划任务,所以需 ...
- php 月初,月末时间大统计
//PHP获取指定月份的月初月尾时间 //获取上月月初月尾时间: $startday=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,date( ...
- java 集合 Connection 栈 队列 及一些常用
集合家族图 ---|Collection: 单列集合 ---|List: 有存储顺序 , 可重复 ---|ArrayList: 数组实现 , 查找快 , 增删慢 ---|LinkedList: 链表实 ...
- 【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇
原发表于ESRI中国社区,转过来.我的社区帐号:jhlong http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=122097 ----------- ...
- flask初探
为什么我们需要模板 让我们来考虑下我们该如何扩充我们这个小的应用程序. 我们希望我们的微博应用程序的主页上有一个欢迎登录用户的标题,这是这种类型的应用程序的一个"标配".忽略本应用 ...
- kindeditor在光标处插入编辑器外的数据
页面 <div class="form-group clearfix"> <label class="control-label col-sm-3 co ...
- Tomcat端口被占用错误
所报错误: 严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730013] ????????????? ...
- Mongo DB 2.6 需要知道的一些自身限定
在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...
- Android网络请求通信之Volley
一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...