牛客国庆集训day5 B 电音之王 (大数乘模)
链接:https://www.nowcoder.com/acm/contest/205/B
来源:牛客网
题目描述
听说多听电音能加快程序运行的速度。
定义一个数列,告诉你a0,a1,m0,m1,c,定义an=m0an-1+m1an-2+c对所有n≥ 2。
求
输入描述:
第一行一个整数T(1≤ T≤ 1000),表示数据组数。
每组数据一行7个整数a
0
,a
1
,m
0
,m
1
,c,M,k,保证1≤ M≤ 10
18
,0≤ a
0
,a
1
,m
0
,m
1
,c< M, 2≤ k≤ 10
6
,保证M为奇数。
保证
。
输出描述:
对于每组数据,输出一行表示答案。
输入例子:
1
1 1 1 1 0 1000000007 10
输出例子:
904493530
-->
输出
904493530 蒙哥马利板子题。
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef unsigned long long u64;
typedef __int128_t i128;
typedef __uint128_t u128; 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; for (int i = ; i < ; ++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; int t, k;
u64 A0, A1, M0, M1, C, M; void Run()
{
scanf("%d", &t);
while (t--)
{
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();
ans *= a0; ans *= a1;
for (int i = ; i <= k; ++i)
{
a2 = a1;
a1 = m0 * a1 + m1 * a0 + c;
a0 = a2;
ans *= a1;
}
printf("%llu\n", ans.get());
}
} int main()
{
Run();
return ;
}
牛客国庆集训day5 B 电音之王 (大数乘模)的更多相关文章
- 牛客国庆集训day5 G 贵族用户 (模拟)
链接:https://www.nowcoder.com/acm/contest/205/G来源:牛客网 题目描述 终于活成了自己讨厌的样子. 充钱能让你变得更强. 在暖婊这个游戏里面,如果你充了x元钱 ...
- 2019牛客国庆集训派对day5
2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...
- 牛客国庆集训派对Day6 A Birthday 费用流
牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...
- 牛客国庆集训派对Day5 Solution
A 璀璨光滑 留坑. B 电音之王 蒙特马利大数乘模运算 #include <bits/stdc++.h> using namespace std; typedef long ...
- 牛客国庆集训派对Day1 L-New Game!(最短路)
链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day4 J-寻找复读机
链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)
链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day1-C:Utawarerumono(数学)
链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day2 Solution
A 矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...
随机推荐
- 2014.10.1 Spy找对话框
IntPtr Diaw =FindWindow("#32770","Adobe Acrobat" );
- wamp集成环境下mysql数据库的分开部署和远程访问
今天折腾了一天一个小问题,就是明明正确的php代码在访问数据库的时候总是提示DB ERROR.后来才发现是填写数据库名的时候,写成了该数据库的ip地址(其实也是本机ip但是本机还是不能访问),而不是l ...
- Python之POST登录测试
不解释,直接上代码: #!/usr/bin/env python # -*- encoding: utf-8 -*- """ @version: v1.0 @author ...
- Enumeration与Iterator的对比
Enumeration与Iterator的对比 Enumeration 接口 Iterator 接口 参数的含义 枚举类型 迭代器元素类型 所在包 java.util 父类 无 子类 StringTo ...
- 基于C++任意点数的FFT/IFFT(时域和频域)实现
函数说明:更改主函数体中的N和length(=log2(N))既可以实现任意点数(2的幂次)的FFT/ IFFT的实现,fft函数中flag标志位控制是正变换还是逆变换. 1.复数操作类 定 ...
- 给Activity切换过程添加动画效果
首先,在资源文件中定义一些动画效果 例如: <scale android:duration="@android:integer/config_mediumAnimTime" ...
- 主机(windows)与VMware虚拟机(linux)互传文件
网上有不少教程,比如http://bbs.kafan.cn/thread-451327-1-1.html,我仅记录我平时用的方法. 1.VMware tools: 如果有网,且Linux有桌面的,可 ...
- JavaScript的作用域与闭包
JavaScript的作用域以函数为界,不同的函数拥有相对独立的作用域.函数内部可以声明和访问全局变量,也可以声明局部变量(使用var关键字,函数的参数也是局部变量),但函数外部无法访问内部的局部变量 ...
- p3253 [JLOI2013]删除物品
传送门 分析 我们发现两个栈可以看作一个数组,而栈顶则是将这个数组拆成两个栈的分割点. 于是每次移动就变成了分割点的移动,每次移动时都统计下目的分割点和当前分割点之间的物品数目即可. 代码 #incl ...
- Entity Framework Tutorial Basics(3):Entity Framework Architecture
Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...