/* Code by
__ __ __
/\ \__ /\ \ /\ \
__ _ __\ \ ,_\ \ \ \___ __ ___\ \ \/'\
/'__`\ /\`'__\ \ \/ \ \ _ `\ /'__`\ /'___\ \ , <
/\ \L\.\_\ \ \/ \ \ \_ \ \ \ \ \/\ \L\.\_/\ \__/\ \ \`\
\ \__/.\_\ \_\ \ \__\ \ \_\ \_\ \__/.\_\ \____\ \_\ \_\
\/__/\/_/ \/_/ \/__/ _______\/_/\/_/\/__/\/_/\/____/ \/_/\/_/
/\______\
\/______/
*/
#include "bits/stdc++.h"
using namespace std;
#define max(a, b) (a < b? b : a)
#define min(a, b) ((a>b)?b:a)
#define mod 1e9+7
#define FOR(a,c) for ( int (a)=0; (a)<(c); (a)++)
#define FORL(a,b,c) for ( int (a)=(b); (a)<=(c); (a)++)
#define FORR(a,b,c) for ( int (a)=(b); (a)>=(c); (a)--)
#define INF 1000000000000000003
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair
#define MOD 1000000007
ll k=2;
vector<ll> b; // b is base case
vector<vector<ll> > multiply(vector<vector<ll> > A,vector<vector<ll> > B ){
vector<vector<ll> > C(k,vector<ll>(k,0));
FOR(i,k)
FOR(j,k)
FOR(x,k)
C[i][j] = ((C[i][j]%MOD) + ((A[i][x]%MOD)*(B[x][j]%MOD))%MOD)%MOD;
return C;
}
vector<vector<ll> > pow(vector<vector<ll> > A,ll p){
if(p==1)
return A;
if(p%2==1)
return multiply(A, pow(A,p-1));
else{
vector<vector<ll> > X = pow(A,p/2);
return multiply(X,X);
}
}
ll compute(ll n,ll m){
//Suppose n<=k
if(n<=k){
return b[k-n];
}
//2. Transformation matrix
vector<vector<ll> > T{
{(m-1),(m-1)},
{1,0}
};
// 3. T^n-k
T = pow(T,n-k);
// 4. multiply by initial vector
ll res = 0;
FOR(i,k)
res = (res%MOD + ((T[0][i]%MOD)*(b[i]%MOD))%MOD)%MOD;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin>>T;
while(T--){
ll N,M;
cin>>N>>M;
if(M==1 && N>1)
cout<<0<<endl;
else if(M==1 && N==1)
cout<<1<<endl;
else{
b.PB((((M%MOD)*(M%MOD))%MOD));
b.PB((M%MOD));
cout<<compute(N,M)<<endl;
b.clear();
}
}
return 0;
}
Modulo is a costly operation and in your multiply function and at some other places you have used unncessary modulo
(a % mod + ((b % mod)*(c % mod)) % modwhich can be replaced with(a + (b * c) % mod) % mod(sincea,b,care long long. In case they are not long long typecast them).+1
even more, just
(a + b * c) % modis enoughI tried that both ways, it gets WA and the TLE remains
Not-Afraid's Way https://www.codechef.com/viewsolution/29577879
Errichto's Way https://www.codechef.com/viewsolution/29577950
Well, you have somebody's correct code so you can easily test your solution locally against it. And compare running time too.
I don't know about WA (maybe your implementation of matrix expo or something else has some bug) but the things creating TLE are:
1)
Instead of using endl use '\n'2)
Pass vector with reference as someone mentioned below.Thanks a lot Errichto and Not-Afraid for your points, there was a minor bug that I removed from your solution and got AC.
Click
P.S. Thanks for the tip that '\n' is faster than endl.
You are passing Vectors by value. Pass them by reference and it should solve the problem.
Does creating a vector and then passing it using its reference have such a significant impact over just passing it?.
yes, everytime you call a function, you make unnecessary copies of it.
Passing without reference creates a new copy of vector every time while this is not the case with reference . So yes it has a significant effect.
Thanks, that did make the difference.