-
Notifications
You must be signed in to change notification settings - Fork 5
/
currency.cpp
126 lines (113 loc) · 2.09 KB
/
currency.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*Subham Sanghai*/
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
long long n;
long long currency[25][25];
void mult(double logA[25][25],double logB[25][25],long long modA[25][25],long long modB[25][25])
{
double logC[n][n];
long long modC[n][n];
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
double sum=0.0;
long long pos;
for(long long k=0;k<n;k++)
{
if((logA[i][k]+logB[k][j])>sum)
{
sum=(logA[i][k]+logB[k][j]);
pos=k;
}
}
logC[i][j]=sum;
modC[i][j]=(modA[i][pos]*modB[pos][j])%mod;
}
}
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
logA[i][j]=logC[i][j];
modA[i][j]=modC[i][j];
//cout<<modA[i][j]<<" ";
}
//cout<<endl;
}
}
void multiply(double logA[25][25],long long modA[25][25],long long k)
{
double logB[25][25];
long long modB[25][25];
for(long long i=0;i<25;i++)
{
for(long long j=0;j<25;j++)
{
modB[i][j]=0;
logB[i][j]=0.0;
}
}
//memset(logB,0.00,sizeof(logB));
//memset(modA,0,sizeof(modA));
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
if(currency[i][j]!=0)
{
logB[i][j]=log(currency[i][j]);
}
else
logB[i][j]=0;
modB[i][j]=currency[i][j];
}
}
if(k==0 || k==1)
return ;
multiply(logA,modA,k/2);
mult(logA,logA,modA,modA);
if(k%2!=0)
mult(logA,logB,modA,modB);
}
int main()
{
cin>>n;
long long x,s,f,m;
cin>>x>>s>>f>>m;
double logA[25][25];
long long modA[25][25];
for(long long i=0;i<25;i++)
{
for(long long j=0;j<25;j++)
{
modA[i][j]=0;
logA[i][j]=0.0;
}
}
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
cin>>currency[i][j];
if(currency[i][j]!=0)
{
logA[i][j]=log(currency[i][j]);
}
else
logA[i][j]=0;
modA[i][j]=currency[i][j];
}
}
multiply(logA,modA,m);
/*for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
cout<<modA[i][j]<<" ";
}
cout<<endl;
}*/
cout<<(x*modA[s][f])%mod<<"\n";
}