#include <list>

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jnxxhzz/article/details/81699956

#include &lt;list&gt;


Time Limit: 2 Seconds      Memory Limit: 65536 KB


   For a sequence X. Xn+1 = ( a * Xn + c ) % p. Now given a, c, X0, n, p, you need to calculate Xn.

Input

The first line contains an integer T, indicating that there are T test cases (T <= 20); 
For each case: 
The only line of the input contains five integers a , c , X0 , n , p. ( a,c,p <= 1e9 , X0,n <= 1e18 )

Output

For each test case, output the integer Xn.

Sample Input

4
4 3 1 1 7
4 3 7 0 3
4 3 7 1 3
2018 7 15 8 31

Sample Output

0
7
1
10

【分析】

题意:求数列X_n=(aX_(n -1)+c)  % p

矩阵快速幂,A也不难构造

  [a,c]

  [0,1]

模板一挂就做完了...

【代码】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <deque>
#include <algorithm>
#include <assert.h>
using namespace std;
#define clr(a,b) memset(a, b, sizeof(a))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define lson(x) (x << 1)
#define rson(x) (x << 1 | 1)
typedef long long LL;
typedef vector<LL> VI;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const LL mod = 1000000007;
LL powmod(LL a,LL b) {LL res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL gcd(LL a,LL b) { return b?gcd(b,a%b):a;}
LL mod_inv(LL a, LL k) {return powmod(powmod(a, k), mod - 2) % mod;}
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
//std::ios::sync_with_stdio(false);
// head

struct Matrix{
    LL a[2][2];
    Matrix(){memset(a,0,sizeof(a));}
    void init(){
        for(int i=0;i<2;++i){
            a[i][i] = 1;
        }
    }
};

Matrix mul(Matrix a, Matrix b){
    Matrix ans;
    for(int i = 0 ; i < 2 ; ++i){
        for(int j = 0 ; j < 2 ; ++j){
            ans.a[i][j] = 0;
            for(int k = 0 ; k <  2 ; ++k){
                ans.a[i][j] += (a.a[i][k] * b.a[k][j]) % p;
                ans.a[i][j] %= p; 
            }
        }
    } 
    return ans;
}

Matrix qpow(Matrix a, int n){
    Matrix ans;
    ans.init();
    while(n){
        if(n&1) ans = mul(ans, a);
        a = mul(a, a);
        n >>= 1;
    } 
    return ans;
}

void output(Matrix a){
    for(int i=0;i<2;++i){
        for(int j=0;j<2;++j){
            cout << a.a[i][j] << " ";
        }
        cout << endl;
    }
}

int main()
{
    int pp;scanf("%d",&pp);
    LL a,c,x0;
    while (pp--){        
        scanf("%lld%lld%lld%lld%lld",&a,&c,&x0,&n,&p);
        if (n == 0) printf ("%lld\n",x0);
        else{
            Matrix TMP;
            TMP.a[0][0] = a;
            TMP.a[0][1] = c;
            TMP.a[1][1] = 1;
            Matrix ANS = qpow(TMP,n);
            LL ans = ((x0 % mod * ANS.a[0][0] % p) % p + ANS.a[0][1]) % p; 
            printf ("%lld\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jnxxhzz/article/details/81699956