题目:

电音之王


题解:

求数列前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());
}
}

参考:

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

除算・剰余算の高速化

Modular arithmetic and Montgomery form 实现快速模乘的更多相关文章

  1. Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Modular arithmetic is a fundamental tool in modern algebra systems. In conjunction wi ...

  2. tourist's modular arithmetic class

    #include <bits/stdc++.h> using namespace std; template <typename T> T inverse(T a, T m) ...

  3. 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  4. BZOJ5118:Fib数列2(O1快速模)

    题意:输入N,输出fib(2^N)%1125899839733759.(P=1125899839733759是素数) 思路:欧拉降幂,因为可以表示为矩阵乘法,2^N在幂的位置,矩阵乘法也可以降幂,所以 ...

  5. PY个快速模

    既然这道题是数学题,那就用 PY 吧! 学点东西: print 可以和 c++ 中的 printf 一样快乐的输出格式 另外一点: 这道题可能数据不够强?想想应该有一个 \(0^0 ~\%~ k =0 ...

  6. 数学--数论--HDU1825(积性函数性质+和函数公式+快速模幂+非互质求逆元)

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  7. BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】

    A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...

  8. 快速幂模n运算

    模运算里的求幂运算,比如 5^596 mod 1234, 当然,直接使用暴力循环也未尝不可,在书上看到一个快速模幂算法 大概思路是,a^b mod n ,先将b转换成二进制,然后从最高位开始(最高位一 ...

  9. 模反元素 RSA Euler's totient function

    https://baike.baidu.com/item/模反元素/20417595 如果两个正整数a和n互质,那么一定可以找到整数b,使得 ab-1 被n整除,或者说ab被n除的余数是1.这时,b就 ...

随机推荐

  1. upc组队赛17 Bits Reverse【暴力枚举】

    Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...

  2. 搭建用例管理平台phpstudy+testlink代替Apache+MySQL+PHP环境,以及testlink搭建环境报错修复

    公司需要搭建一个用例管理平台,环境搭建Apache+MySQL+PHP环境 哇一看就是需要花很长时间去搭建环境,本来我也在用这样的笨方法,不小心被公司开发看到,经人家一提点,哎呀妈呀发现自己以前的方法 ...

  3. webpack2.0 基本使用

    webpack是一款前端模块打包工具, 它的出现是由于现代web开发越来越复杂,如果还是像原来那样把所有的js代码都写到一个文件中,维护非常困难.而解决复杂化的方法通常是分而治之,就是把复杂化的东西进 ...

  4. luogu P3657 (NOIP2017) 跳房子(二分+DP+单调队列)

    题面 传送门 分析 显然答案有单调性,可以二分答案,设当前二分值为g,根据题意我们可以求出跳跃长度的范围[l,r] 考虑DP 子状态: dp[i]表示跳到第i个点时的最大和 状态转移方程 \(dp[i ...

  5. Tarjan&2-SAT 总结

    \(Tarjan\)&\(2-SAT\) 标签: 知识点总结 安利XZYXZY ps:里面的部分东西来自\(Anson\)和\(yler\)和\(XZY\) 阅读体验:https://zybu ...

  6. ubuntu安装supervisor以及使用supervisor启动.net core进程

    1.下载.net core项目ubuntu系统运行容器dotnet      1.版本:dotnet-sdk-2.1.3-linux-x64.tar.gz      2.将下载好的包上传到ubuntu ...

  7. tornado ioloop current和instance的一些区别

    import tornado.ioloop # 此时_current没有instance print dir(tornado.ioloop.IOLoop._current) # 通过instance ...

  8. Django 项目环境搭建

    环境 mkvirtualenv -p python3.6 mytest # 创建虚拟环境 workon mytest # 进入虚拟环境 # 按照基础环境依赖 pip install django==1 ...

  9. Linux学习笔记之磁盘与文件系统的管理

    三.Linux磁盘与文件系统的管理 MBR扇区(512B) 磁盘的分区组成 Boot sector    扇区(用来装引导程序) Super block   记录inode与Block的信息 Inod ...

  10. 英伟达GPU 嵌入式开发平台

    英伟达GPU  嵌入式开发平台 1.         JETSON TX1 开发者组件 JETSON TX1 开发者组件是视觉计算的全功能 开发平台,旨在让您能够快速地安装和运行. 该组件带有 Lin ...