牛客国庆集训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 ...
随机推荐
- Canopy聚类算法(经典,看图就明白)
只有这个算法思想比较对,其他 的都没有一开始的remove: 原网址:http://www.shahuwang.com/?p=1021 Canopy Clustering 这个算法是2000年提出来的 ...
- delphi 从 TWebbrowse组件中获取图片
在 delphi 中使用 TWebbrowse 组件,虽然效率不如用(idhttp之类)模拟操作效率高.但其难度低,上手快,简单粗暴有效. 从网上搜到的处理此问题的文章大多是 ctrl + c 复制到 ...
- When install ”matplotlib” with ”pip”, if you get the following error, it means the “freetype” and “png” libraries needed by matplotlib are not installed:
============================================================================ * The following require ...
- select 动态添加option函数
转自:https://lym6520.iteye.com/blog/309937 经常会用到select动态添加元素,写了个方法,方便调用! ... /** * 功能:select对象动态添加Opt ...
- LAMP 3.5 mysql备份与恢复
备份库的命令 mysqldump -uroot -pwangshaojun discuz > /data/discuz.sql 指定用户密码,重定向到某文件 恢复 mysql -uroot -p ...
- object类型对象 ref参数如何理解?
class Program { static void Main(string[] args) { Student stu = new Student { Name = "老王" ...
- LaTeX 控制图片的位置
加感叹号来忽略“美学”标准. \begin{figure}[!htb] \usepackage{float}\begin{figure}[H]插到你代码相应的位置. 1,插入并列的子图 \usepac ...
- 进度条控件JProgressBar的使用
----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.j ...
- Ros学习——创建程序包
1.程序包 一个程序包要想称为catkin程序包必须符合以下要求: 该程序包必须包含catkin compliant package.xml文件 这个package.xml文件提供有关程序包的元信 ...
- ROS Learning-007 beginner_Tutorials ROS节点
ROS Indigo beginner_Tutorials-06 ROS节点 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...