URAL 1507 Difficult Decision 矩阵快速幂

1507. Difficult Decision

Time limit: 1.0 second

Memory limit: 64 MB

Often, when a decision about investing in a new business must be taken, a New Russian has to estimate quickly whether a certain project will be a success or not. Leading economists have recently discovered
a new algorithm for forecasting the success of a project.

First, one has to form an n × n matrix of risks. Let us denote this matrix by A. Then, in order to take into account the interdependencies of the parameters inside the matrix,
the matrix

must be computed. If at least one of the elements of the matrix B is zero, then there is a considerable probability that the project will fail. Otherwise, if there are no zero elements in the
matrix B, the new business will grow and flourish.

Help New Russians to make use of this algorithm. Your task is to write a program that determines the probability of the success of a project given the matrix of its risks.

Input

The first line of the input contains the dimension n of the matrix A (2 ≤ n ≤ 50). Each of the next n lines contains n numbers that forms the matrix A.
Each element is a whole number in the range from 0 to 100.

Output

Output "No" if there is at least one zero element in the matrix B (so it is better not to invest in the new business). Otherwise, output "Yes".

Samples

input output
2
0 7
15 30
Yes
3
100 35 40
0 22 0
10 11 0
No

Problem Author: Evgeny Krokhalev

Problem Source: Quarter-Final of XXXI ACM ICPC - Yekaterinburg - 2006

Tags: none  (

hide tags for unsolved problems
)

题意:

输入A矩阵,问

,求出的B矩阵是否有0,有的话NO,没有YES。

做法:

矩阵快速幂,先算出 K等于n(n-1)次的A矩阵。复杂度 是 log(n^2)*(n^3)=10^4  ,然后k循环加到 n(n+1),每次把矩阵再乘个A,然后加到B里。复杂度是 n*n^3=10^6。所以妥妥的。因为只在乎有没有0,输入只有正数,矩阵里也只有乘法和加法。所以我把非零数改成了1,然后乘法用状压位运算优化到n^2。跑得稍微快点。

#include<stdio.h>
#include<string.h>
#define Matr 60 //矩阵大小,注意能小就小   矩阵从1开始   所以Matr 要+1   最大可以100
#define ll int
struct mat//矩阵结构体,a表示内容,size大小 矩阵从1开始   但size不用加一
{
    ll a[Matr][Matr];
    mat()//构造函数
    {
        memset(a,0,sizeof(a));
    }
};
int Size;
mat add(mat m1,mat m2)
{
	for(int i=1;i<=Size;i++)
	{
		for(int j=1;j<=Size;j++)
		{
			if(m1.a[i][j]||m2.a[i][j])
				m1.a[i][j]=1;
		}
	}
	return m1;
}
mat multi(mat m1,mat m2)//状压,位运算
{
    mat ans=mat(); 

	__int64 mm1[60];//一行的
	__int64 mm2[60];//一列的

	for(int i=1;i<=Size;i++)
	{
		mm1[i]=0;
		for(int j=1;j<=Size;j++)
		{
			mm1[i]<<=1;
			if(m1.a[i][j])
				mm1[i]|=1;
		}
	}

	for(int i=1;i<=Size;i++)//列
	{
		mm2[i]=0;
		for(int j=1;j<=Size;j++)//行
		{
			mm2[i]<<=1;
			if(m2.a[j][i])
				mm2[i]|=1;
		}
	}

	for(int i=1;i<=Size;i++)
	{
		for(int j=1;j<=Size;j++)
		{
			if(mm1[i]&mm2[j])
				ans.a[i][j]=1;
		}
	}

	/*
    for(int i=1;i<=Size;i++)
        for(int j=1;j<=Size;j++)
            if(m1.a[i][j])//稀疏矩阵优化
                for(int k=1;k<=Size;k++)
                    ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k]); //i行k列第j项
					*/
    return ans;
}

mat quickmulti(mat m,ll n)//二分快速幂
{
    mat ans=mat();
    int i;
    for(i=1;i<=Size;i++)ans.a[i][i]=1;
    while(n)
    {
        if(n&1)ans=multi(m,ans);//奇乘偶子乘 挺好记的.
        m=multi(m,m);
        n>>=1;
    }
    return ans;
}

void print(mat m)//输出矩阵信息,debug用
{
    int i,j;
    printf("%d\n",Size);
    for(i=1;i<=Size;i++)
    {
        for(j=1;j<=Size;j++)
			printf("%d ",m.a[i][j]);
        printf("\n");
    }
}  

int judge(mat m)
{

	for(int i=1;i<=Size;i++)
	{
		for(int j=1;j<=Size;j++)
		{
			if(m.a[i][j]==0)
				return 0;
		}
	}
	return 1;
}
int main()
{
	mat gouzao=mat(),chu=mat();//构造矩阵  初始矩阵
	mat ans=mat();

	cin>>Size;

	for(int i=1;i<=Size;i++)
	{
		for(int j=1;j<=Size;j++)
		{
			int tem;
			scanf("%d",&tem);
			if(tem)
				gouzao.a[i][j]=1;
		}
	}

	chu=quickmulti(gouzao,Size*(Size-1)-1);

	for(int i=Size*(Size-1);i<=Size*(Size+1);i++)
	{
		chu=multi(chu,gouzao);
		ans=add(ans,chu);
	}

	if(judge(ans))//mei 0
		puts("Yes");

	else
		puts("No");
	return 0;
}
时间: 2024-10-23 11:31:17

URAL 1507 Difficult Decision 矩阵快速幂的相关文章

ural 1507 Difficult Decision

 H - Difficult Decision Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1507 Description Often, when a decision about investing in a new business must be taken, a New Russian has to estimate

URAL 1507. Difficult Decision(矩阵快速幂)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1507 Often, when a decision about investing in a new business must be taken, a New Russian has to estimate quickly whether a certain project will be a success or not. Leading economists have recently

2016 pku campusH/OpenJ_POJ - C16H(推公式+矩阵快速幂)

传送门:http://poj.openjudge.cn/practice/C16H?lang=en_US 题面:描述 Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever. Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k