poj 2947 Widget Factory
题意:有n件装饰品,有m组信息。(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1~n),其中每种装饰品所用的时间3 <= x[i] <= 9;种类的输入可以重复;
思路:
1.根据输入建立增广矩阵a[][],但是在建立和求解的过程中由于是mod意义下的,所以输入的个数和最终所用的时间都要mod 7;(分析可知当个数是7的同余类时,开始星期相同则结束星期也相同)
2.前面几个高斯消元,我用的是free_var来判断是否有自由变元,这是在输入的方程数和求解变元数相等的情况才成立。在本题中对于sample 1就会发现方程数原本就比变元多1,这时计算出的free_var = 1,但是并不是将就有了一个维度的自由变元。还是要看有用方程的个数row与var之间的关系;
3.在得到上三角阵求解变元x[i]的时候,需要解一个模线性方程,a[i][i]*x[i] + 7*y = ret(mod 7);ret为a[i][col]用已知的x[j]消去除a[i][i]得到的;
这时调用exgcd()即可求解;最后注意解要在3~9范围内即可;
ps:时间性能不是很好,竟然用了1704ms...最短的是297ms..差距啊!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
int a[][];
int equ,var;
int x[];
const int MOD = ;
template <typename T>
T abs(T a){return a < ? -a:a;}
void debug()
{
puts("********");
int i,j;
rep0(i,,equ){
rep1(j,,var)
cout<<a[i][j]<<" ";
cout<<endl;
}puts("********");
}
int __gcd(int a,int b)
{
return b?__gcd(b,a%b):a;
}
int LCM(int a,int b)
{
return a/__gcd(a,b)*b;
}
void exgcd(int a,int b,int& d,int& x,int& y)
{
if(!b){d = a;x = ;y = ;}
else{
exgcd(b,a%b,d,y,x);
y -= x*(a/b);
}
}
int Guass()
{
int i,j,k,free_var = ,row,col;
for(row = ,col = ;row < equ && col < var;row++,col++){
int mx = row;
rep0(j,row+,equ)
if(abs(a[j][col]) > abs(a[mx][col])) mx = j;
if(a[mx][col] == ){
row--; // 行不变;不能通过这里记录自由变元的个数,只能记录没用的col
continue;
}
if(mx != row)
rep1(k,col,var)
swap(a[row][k],a[mx][k]);
rep0(j,row+,equ){
if(a[j][col]){
int lcm = LCM(abs(a[row][col]),abs(a[j][col]));
int ration_row = lcm/abs(a[row][col]),ration_j = lcm/abs(a[j][col]);
if(a[row][col]*a[j][col] < ) ration_row = -ration_row; //符号相反变加法;
rep1(k,col,var)
a[j][k] = (a[j][k]*ration_j - a[row][k]*ration_row)%;
}
}
}
//debug();
rep0(i,row,equ)
if(a[i][var] != ) return -; //无解
if(row < var) return var - row;//row表示有用的方程数方程,但是要在判断出有解的前提下才能说有多组解;
rep_1(i,var - ,){ // ***若为唯一解,其实就是var维方阵
int ret = a[i][var];
for(j = i+;j < var;j++) //利用已求得的变元消去第row行col后面的元素,得到一元方程;
ret -= x[j]*a[i][j];
ret = ((ret%)+)%;
int d,x1,y;
//构造出 a[i][i]*x[i] + 7*y = ret(mod 7);且gcd(a[row][col],7) = 1)因为a[row][col] != 0
if(a[i][i] < ) a[i][i] = -a[i][i],ret = -ret;
exgcd(a[i][i],,d,x1,y); //之后乘上ret弄到3~9范围即可;
x[i] = ((ret*x1)%+)%;
if(x[i] < ) x[i] += ;
}
return ;
}
const char str[][] = {{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};
int date_id(char *c)
{
for(int i = ;i < ;i++)
if(strcmp(str[i],c) == ) return i;
}
int main()
{
int i,j,n,m;
char s[],t[];
while(scanf("%d%d",&n,&m) == && n + m){
MS0(a);
equ = m;var = n;
int kind,num;
rep0(i,,m){
scanf("%d%s%s",&num,s,t);
a[i][var] = date_id(t)-date_id(s)+;
if(a[i][var] < ) a[i][var] += ;
rep0(j,,num){
scanf("%d",&kind);
a[i][--kind]++;
}
rep1(j,,var) a[i][j] %= ;
}
//debug();
int ret = Guass();
if(ret == -) puts("Inconsistent data.");
else if(ret > ) puts("Multiple solutions.");
else{
rep0(i,,var)
printf("%d%c",x[i],i == var - ?'\n':' ');
}
}
return ;
}
poj 2947 Widget Factory的更多相关文章
- POJ 2947 Widget Factory(高斯消元)
Description The widget factory produces several different kinds of widgets. Each widget is carefully ...
- poj 2947 Widget Factory (高斯消元解同余方程组+判断无解、多解)
http://poj.org/problem?id=2947 血泪史: CE:poj的string类型要加string库,swap不能直接交换数组 WA: x[m-1]也有可能<3啊O(≧口≦) ...
- Poj 2947 widget factory (高斯消元解同模方程)
题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...
- POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)
题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...
- 【POJ】2947 Widget Factory(高斯消元)
http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...
- POJ 2947 2947 Widget Factory 高斯消元
给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...
- POJ Widget Factory 【求解模线性方程】
传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- POJ 2947:Widget Factory 求同余方程
Widget Factory Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 5173 Accepted: 1790 De ...
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
随机推荐
- poj 2724 二分图最大匹配
题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...
- asp.net mvc 的几种分页Pager
第一种 /// <summary> /// 分页Pager显示 /// </summary> /// <param name="html">&l ...
- this.class.getClassLoader()怎么理解?
this.class.getClassLoader()怎么理解? java是面向对象语言,面向对象的语言的宗旨就是万事万物皆对象,那么类也是一个对象,类里面的属性和方法也是对象.java里面的所 有的 ...
- android——获取ImageView上面显示的图片bitmap对象
获取的函数方法为:Bitmap bitmap=imageView.getDrawingCache(); 但是如果只是这样写我们得到的bitmap对象可能为null值,正确的方式为: imageView ...
- Android 快捷方式相关操作
尽管现在少数手机不支持快捷方式,但是仍然有大部分手机是支持的.创建快捷方式,可以减少用户在应用列表繁多的应用程序中查找应用的时间,快速进入应用:或是应用中的某个功能使用频率较高,创建快捷方式,可以快速 ...
- JDBC之数据库操作
JDBC重要界面有: java.sgl.DriverManager:完成驱动程序的装载和建立新的数据库连接. java.sgl.Connection:表示对某一指定数据库的连接. java.sgl.S ...
- Excel数据生成Sql语句的方法
选中想要生成的列,套用表格格式,选中表包含标题的选项确定,然后在最右边的一列第二行处,点击函数功能,选择CONCATENATE,在文本里输入想要的结构即可 代码如下 复制代码 ,=CONCATENA ...
- jQuery中的getter和setter方法
1.attr()方法是jQuery中用于HTML属性的getter/setter.一个相关函数是removeAttr(). 2.css()方法和attr()方法很类似,只是css()方法作用于元素的c ...
- 【leetcode】363. Max Sum of Rectangle No Larger Than K
题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...
- C++四种强制类型转换关键字
C语言使用强制类型转换(Type Cast)很简单,不管什么类型的转换,形式都如下: TYPE b = (TYPE)a; c++提供了4种类型转换操作符来应对不同场合的应用. const_cast ...