#include <list>

4 篇文章 0 订阅

#include <list>


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;
}

 

#include #include struct DATA { int ID; char name[4]; char sex[3]; int score; }; void paixu(int*,DATA*,int); int sishewuru(double); void func1(int*,int*,DATA*,int*,int,int,int,int);//统计男女比例 int func2(int*,int,DATA*);//查找考生序号 void print(); void main() { int length=0,i,yiben,erben,sanben,dazhuan,male[4],female[4]; int yi,er,san,si; char input; FILE* file=fopen("f1.txt","r"),*file1; if(file==NULL) { printf("No such file!\n"); return; } while(EOF!=fscanf(file,"%*[^\n]\n")) length++;//自动计算考生数罝ATA* data=(DATA*)malloc(length*sizeof(DATA)); int* pai=(int*)malloc(length*sizeof(int)); rewind(file); for(i=0;i='0'&&input&lt;='4')) { printf("非法输入,请重新输入\n请输入:"); fflush(stdin); } else break; } getchar(); switch(input) { case '0': printf("\n一类本科招生线:%d\n二类本科招生线:%d\三类本科招生线:%d\\n高职高专招生线:%d\n",yi,er,san,si); printf("是否打印为文件?(y/n):"); if(getchar()=='y') { file1=fopen("各批次录取分数线.txt","w"); fprintf(file1,"一类本科招生线:%d\n二类本科招生线:%d\\n三类本科招生线:%d\n高职高专招生线:%d\n",yi,er,san,si); fclose(file1); } fflush(stdin); break; case '1': func1(male,female,data,pai,yiben,erben,sanben,dazhuan); printf("一类本科招生线男女比例:%d:%d\n",male[0],female[0]); printf("二类本科招生线男女比例:%d:%d\n",male[1],female[1]); printf("三类本科招生线男女比例:%d:%d\n",male[2],female[2]); printf("高职高专招生线招生线男女比例:%d:%d\n",male[3],female[3]); printf("是否打印为文件?(y/n):"); if(getchar()=='y') { file1=fopen("各批次录取男女比例.txt","w"); fprintf(file1,"一类本科招生线男女比例:%d:%d\n",male[0],female[0]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值