- #include <bits/stdc++.h>
- using namespace std;
- #define llu long long unsigned
- llu a, b, n, c, m, answer;
- llu cm[4][4];
- #define MOD 10007
- void mul(llu a[4][4],llu b[4][4]){
- memset(cm, 0, sizeof(cm));
- for(int i = 0; i <4; i++){
- for(int j = 0; j <4; j++){
- for(int k = 0; k <4; k++){
- cm[i][j]+= ((a[i][k]%MOD)*(b[k][j]%MOD))%MOD;
- cm[j][j]%=MOD;
- }
- }
- }
- for(int i = 0; i <4; i++){
- for(int j = 0; j <4; j++){
- a[i][j] = cm[i][j]%MOD;
- }
- }
- }
- void ans(llu p[4][4], llu n){
- llu m[4][4] = {{a, 0 , b, 1}, {1, 0 , 0, 0}, {0, 1 , 0, 0}, {0, 0 , 0, 1}};
- if(n == 1) return;
- ans(p, n/2);
- mul(p, p);
- if(n%2) mul(p, m);
- }
- int main(){
- int ts;
- scanf("%d", &ts);
- for(int p = 1; p <= ts; p++){
- scanf("%lld%lld%lld%lld", &n,&a, &b, &c);
- llu f[4][4] = {{a, 0 , b, 1}, {1, 0 , 0, 0}, {0, 1 , 0, 0}, {0, 0 , 0, 1}};
- if (n<= 2) answer = 0;
- else ans(f, n-2), answer = (c*f[0][3])%MOD;
- printf("Case %d: %lld\n", p,answer%MOD );
- }
- return 0;
- }
Wednesday, 26 July 2017
lightoj 1096 nth term
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
-
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
No comments:
Post a Comment