golang卡不过去求助
查看原帖
golang卡不过去求助
77366
wongdark2017楼主2020/6/23 20:59

被卡得死死的

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

var in = bufio.NewReader(os.Stdin)

func readInt(out *int) error {
	var ans, sign int = 0, 1
	var readed = false
	c, err := in.ReadByte()
	for ; err == nil && (c < '0' || '9' < c); c, err = in.ReadByte() {
		if c == '-' {
			sign = -sign
		}
	}
	for ; err == nil && '0' <= c && c <= '9'; c, err = in.ReadByte() {
		ans = ans<<3 + ans<<1 + int(c-'0')
		readed = true
	}
	if readed {
		*out = ans * sign
		return nil
	}
	return err
}

var A,ans,c [105][105]int
var mod=int(1e9+7)
var n,k int

func jzcf1(){
	for i := 1; i <= n ; i++ {
		for j := 1; j <=n ; j++ {
			c[i][j]=0
		}
	}
	for k := 1; k <= n ; k++ {
		for i := 1; i <= n ; i++ {
			for j := 1; j <= n ; j++ {
				c[i][j]=(c[i][j]%mod+(ans[i][k]%mod*A[k][j]%mod)%mod)%mod
			}
		}
	}
	for i := 1; i <=n ; i++ {
		for j := 1; j <= n ; j++ {
			ans[i][j]=c[i][j]
		}
	}
}


func jzcf2(){
	for i := 1; i <= n ; i++ {
		for j := 1; j <=n ; j++ {
			c[i][j]=0
		}
	}
	for k := 1; k <= n ; k++ {
		for i := 1; i <= n ; i++ {
			for j := 1; j <= n ; j++ {
				c[i][j]=(c[i][j]%mod+(A[i][k]%mod*A[k][j]%mod)%mod)%mod
			}
		}
	}
	for i := 1; i <=n ; i++ {
		for j := 1; j <= n ; j++ {
			A[i][j]=c[i][j]
		}
	}
}

func main() {
	fmt.Scanf("%d %d\n",&n,&k)
	for i := 1; i <= n ; i++ {
		ans[i][i]=1
		s, _ :=in.ReadString('\n')
		qaq:=strings.Split(s," ")
		for j:=1;j<=n;j++{
			fmt.Sscanf(qaq[j-1],"%d",&A[i][j])
		}
	}
	//fmt.Println(A)
	for k>0 {
		if (k&1)==1{
			jzcf1()
		}
		jzcf2()
		k/=2
	}
	for i := 1; i <=n ; i++ {
		for j := 1; j <=n ; j++ {
			if j==n{
				fmt.Printf("%d\n",ans[i][j])
			}else{
				fmt.Printf("%d ",ans[i][j])
			}
		}
	}
}
2020/6/23 20:59
加载中...