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的更多相关文章

  1. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  2. poj 2947 Widget Factory (高斯消元解同余方程组+判断无解、多解)

    http://poj.org/problem?id=2947 血泪史: CE:poj的string类型要加string库,swap不能直接交换数组 WA: x[m-1]也有可能<3啊O(≧口≦) ...

  3. Poj 2947 widget factory (高斯消元解同模方程)

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  4. POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

    题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...

  5. 【POJ】2947 Widget Factory(高斯消元)

    http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...

  6. POJ 2947 2947 Widget Factory 高斯消元

    给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...

  7. POJ Widget Factory 【求解模线性方程】

    传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  8. POJ 2947:Widget Factory 求同余方程

    Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 5173   Accepted: 1790 De ...

  9. 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)

    使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...

随机推荐

  1. Android 高级UI设计笔记03:使用ListView实现左右滑动删除Item

    1. 这里就是实现一个很简单的功能,使用ListView实现左右滑动删除Item: (1)当我们在ListView的某个Item,向左滑动显示一个删除按钮,用户点击按钮,即可以删除该项item,并且有 ...

  2. C#打开指定目录,并将焦点放在指定文件上。相对路径(程序起动的目录)

    string basepath = AppDomain.CurrentDomain.BaseDirectory; string filepath = "logs\\Log.log" ...

  3. javaweb学习总结十八(软件密码学、配置tomcat的https连接器以及tomcat管理平台)

    一:软件密码学 1:对称加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key).对称加密有很多种算法,由于它效 ...

  4. Spring AOP原理解析

    原文链接请参见:http://blog.csdn.net/u010723709/article/details/47839307

  5. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

  6. 【转】六年软件测试感悟-从博彦到VMware

    不知不觉已经从事软件测试六年了,2006毕业到进入外包公司外包给微软做软件测试, 到现在加入著名的外企.六年的时间过得真快. 长期的测试工作也让我对软件测试有了比较深入的认识.但是我至今还是一个底层的 ...

  7. ASP、JSP、PHP 三种技术比较

    目前,最常用的三种动态网页语言有ASP(Active Server Pages),JSP(JavaServer Pages),PHP (Hypertext Preprocessor). 简 介 : A ...

  8. WCF配置文件详解 【转】

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  9. HTTP - 条件请求

    当 HTTP 请求包含 If-XXX 这种样式的首部时,服务器会对附带的条件进行判断,只有判断指定条件为真,才会执行请求.这样的请求首部有五个,分别是 If-Modified-Since.If-Unm ...

  10. Linux下Tomcat启动正常,但浏览器无法访问

    1.服务器可ping通 2.服务器抓本地的http请求包,可以抓到 3.本地抓服务器返回的http响应包,抓不到 经过查找,是由于开启了Linux防火墙 查看防火墙配置(需要root权限) [root ...