题目:

电音之王


题解:

求数列前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. 【Unity 系统知识】 各种路径

    一.Assets下的Resources(Unity系统文件夹) :路径 Application.dataPath/Resources 可以使用Resources.Load("文件名字,注:不 ...

  2. 解决BootstrapTable设置height属性后,表格不对齐的问题

    解决BootstrapTable设置height属性后,表格不对齐的问题 2018年03月06日 09:56:54 nb7474 阅读数 5920     一般在使用BootstrapTable 插件 ...

  3. Reinforcement Learning for Self Organization and Power Control of Two-Tier Heterogeneous Networks

    R. Amiri, M. A. Almasi, J. G. Andrews and H. Mehrpouyan, "Reinforcement Learning for Self Organ ...

  4. C# 栈=>随时读取栈中最小值

    //原理:利用两个栈,一个记录最小值,一个记录数据. using System; using System.Collections.Generic; using System.Linq; using ...

  5. 使用Angular2+的内置管道格式化数据

    在简书看到一篇关于Angualr运用内置管道格式化数据的总结,感觉挺实用的,转载一下以供参考: [转载]https://www.jianshu.com/p/a8bd5a1d2c53 PS:管道是在HT ...

  6. 18、NumPy——矩阵库(Matrix)

    NumPy 矩阵库(Matrix) NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 一个 的矩阵是一个由行(row)列(col ...

  7. GeneXus笔记本——部分环境属性设置项

    这些属性的设置是我们在做项目的过程中都会设置的属性 当然也因项目而异 这里也只是单纯的记录一下 知识库 属性设置“Maximun numeric length" 效果:设置数值型最大值 版本 ...

  8. 2018-2-13-win10-uwp-BadgeLogo-颜色

    title author date CreateTime categories win10 uwp BadgeLogo 颜色 lindexi 2018-2-13 17:23:3 +0800 2018- ...

  9. Redis这篇就够了

    Redis 简介 Redis 优势 Redis 数据类型 string hash list set Zset 小总结 基本命令 发布订阅 简介 实例 发布订阅常用命令 事务 实例 Redis 事务命令 ...

  10. mangodb语句

    { field: { $exists: <boolean> } }