电音之王 对64bit int 取模
生活随笔
收集整理的這篇文章主要介紹了
电音之王 对64bit int 取模
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:https://www.nowcoder.com/acm/contest/205/B
來源:牛客網
電音之王
時間限制:C/C++ 2秒,其他語言4秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
終于活成了自己討厭的樣子。
聽說多聽電音能加快程序運行的速度。
定義一個數列,告訴你a0,a1,m0,m1,c,定義an=m0an-1+m1an-2+c對所有n≥ 2。
求
輸入描述:
第一行一個整數T(1≤ T≤ 1000),表示數據組數。 每組數據一行7個整數a0,a1,m0,m1,c,M,k,保證1≤ M≤ 1018,0≤ a0,a1,m0,m1,c< M, 2≤ k≤ 106,保證M為奇數。 保證。輸出描述:
對于每組數據,輸出一行表示答案。示例1
輸入
復制
1 1 1 1 1 0 1000000007 10輸出
復制
904493530 #include <stdio.h> #include<vector> #include<utility> #include <assert.h> using namespace std; #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 SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007;ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // headtypedef unsigned long long u64; typedef __int128_t i128; typedef __uint128_t u128; int _,k; u64 A0,A1,M0,M1,C,M;struct Mod64 {Mod64():n_(0) {}Mod64(u64 n):n_(init(n)) {}static u64 init(u64 w) { return reduce(u128(w) * r2); }static void set_mod(u64 m) {mod=m; assert(mod&1);inv=m; rep(i,0,5) inv*=2-inv*m;r2=-u128(m)%m;}static u64 reduce(u128 x) {u64 y=u64(x>>64)-u64((u128(u64(x)*inv)*mod)>>64);return ll(y)<0?y+mod:y;}Mod64& operator += (Mod64 rhs) { n_+=rhs.n_-mod; if (ll(n_)<0) n_+=mod; return *this; }Mod64 operator + (Mod64 rhs) const { return Mod64(*this)+=rhs; }Mod64& operator -= (Mod64 rhs) { n_-=rhs.n_; if (ll(n_)<0) n_+=mod; return *this; }Mod64 operator - (Mod64 rhs) const { return Mod64(*this)-=rhs; }Mod64& operator *= (Mod64 rhs) { n_=reduce(u128(n_)*rhs.n_); return *this; }Mod64 operator * (Mod64 rhs) const { return Mod64(*this)*=rhs; }u64 get() const { return reduce(n_); }static u64 mod,inv,r2;u64 n_; }; u64 Mod64::mod,Mod64::inv,Mod64::r2;int main() {for (scanf("%d",&_);_;_--) {scanf("%llu%llu%llu%llu%llu%llu%d",&A0,&A1,&M0,&M1,&C,&M,&k);Mod64::set_mod(M);Mod64 a0(A0),a1(A1),m0(M0),m1(M1),c(C),ans(1),a2(0);for (int i=0;i<=k;i++) {ans=ans*a0;a2=m0*a1+m1*a0+c;a0=a1; a1=a2;}printf("%llu\n",ans.get());} }?
總結
以上是生活随笔為你收集整理的电音之王 对64bit int 取模的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1599 find the min
- 下一篇: Linux文件系统层次结构