题目:

电音之王


题解:

求数列前n项相乘并取模


思路:

①、这题的乘法是爆long long的,可以通过快速幂的思想去解决(按数位对其中的一个数进行剖分)。当然你的乘法会多出一个log的复杂度...

②、O(1)快速乘:一种O(1)复杂度求解整数相乘取模的思路(它对于64位的整型也是适用的):

  来自2009年国家集训队论文:骆可强:《论程序底层优化的一些方法与技巧》 (参考中附原文链接)

typedef long long ll;
#define MOL 123456789012345LL inline ll mul_mod_ll(ll a,ll b)
{
ll d = (ll)floor(a * (double)b / MOL + 0.5);
ll ret = a * b - d * MOL;
if(ret < ) ret += MOL;
return ret;
}

③、正解dls一句话题解(当然是看不懂了...)

  参考中附一篇Montgomery Modular Multiplication的博客(当然也是看不懂了...日文)

题解:(dls的代码)

#include <bits/stdc++.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=;
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head typedef 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_() {}
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&);
inv=m; rep(i,,) inv*=-inv*m;
r2=-u128(m)%m;
}
static u64 reduce(u128 x) {
u64 y=u64(x>>)-u64((u128(u64(x)*inv)*mod)>>);
return ll(y)<?y+mod:y;
}
Mod64& operator += (Mod64 rhs) { n_+=rhs.n_-mod; if (ll(n_)<) n_+=mod; return *this; }
Mod64 operator + (Mod64 rhs) const { return Mod64(*this)+=rhs; }
Mod64& operator -= (Mod64 rhs) { n_-=rhs.n_; if (ll(n_)<) 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; u64 pmod(u64 a,u64 b,u64 p) {
u64 d=(u64)floor(a*(long double)b/p+0.5);
ll ret=a*b-d*p;
if (ret<) ret+=p;
return ret;
} void bruteforce() {
u64 ans=;
for (int i=;i<=k;i++) {
ans=pmod(ans,A0,M);
u64 A2=pmod(M0,A1,M)+pmod(M1,A0,M)+C;
while (A2>=M) A2-=M;
A0=A1; A1=A2;
}
printf("%llu\n",ans);
} 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(),a2();
for (int i=;i<=k;i++) {
ans=ans*a0;
a2=m0*a1+m1*a0+c;
a0=a1; a1=a2;
}
printf("%llu\n",ans.get());
}
}

参考:

论程序底层优化的一些方法与技巧

除算・剰余算の高速化

最新文章

  1. 阿里云系列——6.给你的域名使用CDN加速(详细步骤+简单配置)
  2. 如何搭建DNS服务
  3. 基于.net mvc 的供应链管理系统(YB-SCM)开发随笔1-开篇
  4. ArcGIS JavaScript + 天地图API之显示混乱
  5. Android(Java):jni源代码
  6. B,B+,B-,B*树
  7. linux服务之varnish
  8. GitHub 上排名前 100 的 Android 开源库进行简单的介绍
  9. [置顶] 《MFC游戏开发》笔记一 系列简介
  10. SQL Server AlwaysOn 和 ILB
  11. TStack,TQueue,TObjectList,TObjectStack等等
  12. IOS学习【前言】
  13. 牛刀小试——记一次帮朋友小幅优化SQL
  14. python学习_2
  15. [福大软工] Z班——个人技术博客评分
  16. centos下配置maven编译环境
  17. MYSQL获取当前年、季、月、周第一天、最后一天的日期/时间戳
  18. Always run a program in administrator mode in Windows 10
  19. Background Media Recovery terminated with ORA-1274 after adding a Datafile (Doc ID 739618.1)
  20. 浅谈 js 对象 toJSON 方法

热门文章

  1. PTA 1154 Vertex Coloring
  2. Android深度探索-卷1第七章心得体会
  3. 《STL源码剖析》——第四章、序列容器
  4. [Python3] 021 面向对象 第一弹
  5. 数据溢出-varchar类型
  6. Pandas的基础使用
  7. vue-devtools工具的安装
  8. Python之反向迭代
  9. 1.ireport基本使用
  10. 20180209-sys模块