bzoj2875

矩阵一开始写惨了。。

 1 #include<bits/stdc++.h>
 2 #define lowbit(a) ((a)&(-(a)))
 3 #define clr(a,x) memset(a,x,sizeof(a))
 4 #define rep(i,l,r) for(int i=l;i<(r);i++)
 5 typedef long long ll;
 6 using namespace std;
 7 ll read()
 8 {
 9     char c=getchar();
10     ll ans=0,f=1;
11     while(!isdigit(c)){
12         if(c=='-') f=-1;
13         c=getchar();
14     }
15     while(isdigit(c)){
16         ans=ans*10+c-'0';
17         c=getchar();
18     }
19     return ans*f;
20 }
21 ll mul(ll a,ll b,ll mod){
22     ll c=0;
23     while(b){
24         if(b&1) c+=a;
25         if(c>=mod) c-=mod;
26         a<<=1;
27         if(a>=mod) a-=mod;
28         b>>=1;
29     }
30     return c;
31 }
32 ll m,a,c,x,n,g;
33 struct matrix{
34     ll a[2][2];
35     inline void clear(){
36         clr(a,0);
37     }
38     inline matrix operator*(const matrix&A){
39         matrix ans;
40         ans.clear();
41         rep(i,0,2){
42             rep(j,0,2){
43                 rep(k,0,2){
44                     ans.a[i][j]=(ans.a[i][j]+mul(a[i][k],A.a[k][j],m))%m;
45                 }
46             }
47         }
48         return ans;
49     }
50     inline matrix operator=(const matrix&A){
51         memcpy(a,A.a,sizeof(a));
52         return *this;
53     }
54     inline matrix operator^(ll k){
55         matrix ans,A=*this;
56         ans.clear();
57         ans.a[0][0]=ans.a[1][1]=1;
58         while(k){
59             if(k&1) ans=ans*A;
60             A=A*A;
61             k>>=1;
62         }
63         return ans;
64     }
65 };
66 matrix A;
67 int main()
68 {    
69     m=read(),a=read(),c=read(),x=read(),n=read(),g=read();
70     A.a[0][0]=a;A.a[0][1]=0;A.a[1][0]=c;A.a[1][1]=1;
71     matrix ans=A^n;
72     cout<<(mul(x,ans.a[0][0],m)+ans.a[1][0])%m%g<<endl;
73     return 0;
74 }
View Code

2875: [Noi2012]随机数生成器

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 1290  Solved: 732
[Submit][Status][Discuss]

Description

 

Input

包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数。

Output

输出一个数,即Xn mod g

Sample Input


11 8 7 1 5 3


Sample Output

2

HINT

 

Source

 
[Submit][Status][Discuss]
posted @ 2015-08-13 15:42  ChenThree  阅读(60)  评论(0编辑  收藏