bzoj2875

题意:\(x_{i+1}=(x_{i}*a+c)%m\)求,x_n%g
题解:\(x_n=(a^n*x_0+(a^{n-1}+a^{n-2}+...+a+1)*c)%m\),由于a-1和m不一定互质,所以没法逆元,只能矩阵快速幂求,乘法必须用快速乘,不然会爆ll

/**************************************************************
    Problem: 2875
    User: walfy
    Language: C++
    Result: Accepted
    Time:52 ms
    Memory:1292 kb
****************************************************************/
 
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
//#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
//inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
//inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
inline ll qm(ll a,ll b,ll c){ll ans=0;while(b){if(b&1)ans=(ans+a)%c;a=(a+a)%c,b>>=1;};return ans;}
 
using namespace std;
 
const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=500000+10,maxn=100000+10,inf=0x3f3f3f3f;
 
struct Node{
    ll row,col;
    ll a[3][3];
};
Node mul(Node x,Node y,ll mod)
{
    Node ans;
    ans.row=x.row,ans.col=y.col;
    memset(ans.a,0,sizeof ans.a);
    for(int i=0;i<x.row;i++)
        for(int j=0;j<x.col;j++)
           for(int k=0;k<y.col;k++)
               ans.a[i][k]=(ans.a[i][k]+qm(x.a[i][j],y.a[j][k],mod)+mod)%mod;
    return ans;
}
Node quick_mul(Node x,ll n,ll mod)
{
    Node ans;
    ans.row=x.row,ans.col=x.col;
    memset(ans.a,0,sizeof ans.a);
    for(int i=0;i<ans.col;i++)ans.a[i][i]=1;
    while(n){
        if(n&1)ans=mul(ans,x,mod);
        x=mul(x,x,mod);
        n/=2;
    }
    return ans;
}
int main()
{
    ll m,a,c,x,n,g;scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x,&n,&g);
    a%=m,c%=m;
    Node A;A.row=A.col=3;
    A.a[0][0]=a,A.a[0][1]=0,A.a[0][2]=1;
    A.a[1][0]=0,A.a[1][1]=a,A.a[1][2]=0;
    A.a[2][0]=0,A.a[2][1]=0,A.a[2][2]=1;
    A=quick_mul(A,n-1,m);
    ll t1=(A.a[0][0]+A.a[0][1]+A.a[0][2])%m,t2=(A.a[1][0]+A.a[1][1]+A.a[1][2])%m;
    t2=qm(t2,a,m);t2=qm(t2,x,m);
    t1=qm(t1,c,m);t1=(t1+t2)%m;
    printf("%lld\n",t1%g);
    return 0;
}
/********************
 
********************/
posted @ 2019-02-27 21:32  walfy  阅读(41)  评论(0编辑  收藏