链接: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

-->

示例1

输入

复制

1
1 1 1 1 0 1000000007 10

输出

复制

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 电音之王 (大数乘模)的更多相关文章

  1. 牛客国庆集训day5 G 贵族用户 (模拟)

    链接:https://www.nowcoder.com/acm/contest/205/G来源:牛客网 题目描述 终于活成了自己讨厌的样子. 充钱能让你变得更强. 在暖婊这个游戏里面,如果你充了x元钱 ...

  2. 2019牛客国庆集训派对day5

    2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...

  3. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  4. 牛客国庆集训派对Day5 Solution

    A    璀璨光滑 留坑. B    电音之王 蒙特马利大数乘模运算 #include <bits/stdc++.h> using namespace std; typedef long ...

  5. 牛客国庆集训派对Day1 L-New Game!(最短路)

    链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  6. 牛客国庆集训派对Day4 J-寻找复读机

    链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  7. 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)

    链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  8. 牛客国庆集训派对Day1-C:Utawarerumono(数学)

    链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  9. 牛客国庆集训派对Day2 Solution

    A    矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...

随机推荐

  1. 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'

    找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.

  2. 2015.3.7 Dll CString不能作为传入参数而要用char*

    extern "C" __declspec(dllexport) void CalcArc_2(Point2D& pm, double am, double an, CSt ...

  3. leetcode559

    class Solution { public: int maxDepth(Node* root) { ; if (root != NULL) { queue<Node> Q; Q.pus ...

  4. 记录一次手机联系人整理(XML文件格式处理)

    场景:1.IOS手机和Android手机联系人同步时有部分重复联系人. 2.很早以前的HTC手机导出的联系人中备注信息有大量乱码,且很多联系人生日被设置为1970-01-01,导致生日提醒软件产生骚扰 ...

  5. ztree 树的模糊搜索

    (转载),有个坑记录下: (原文)实现类似下面这种: /** * 展开树 * @param treeId */ function expand_ztree(treeId) { var treeObj ...

  6. JQuery利用css()修改样式后 hover失效的解决办法

    执行完代码后发现写在样式表中的hover效果失效,改了好几遍差点重新写函数,后来发现很简单,是优先级的问题,css()中的内容覆盖了之前的样式 只需要在样式后写!important即可解决! .fil ...

  7. [转]MySQL5.6.22 安装

    原文路径 http://jifeng3321.iteye.com/blog/2181517?utm_source=tuicool   由于一直做银行项目,所以一直在用oracle和db2,但最近自己想 ...

  8. gearman client的doBackground 与doNormal方法的区别

    doNormal方法是阻塞的,需要等到worker处理完之后才返回,否则一直阻塞住; doBackground 方法是非阻塞的,只要将数据发送到gearmand之后,就立马返回,不等待worker的处 ...

  9. 使用jar打war包或解压war包

    进入Dos命令行,并到目标文件夹,如C:\Temp,待打包的内容在C:\Temp\Blog里,目标,把Blog里的相应文件打成war报 1.打包 C:\Temp\jar -cvf Blog.war . ...

  10. 面试题: 1天的java面试题 已看1

    1,自我介绍下,我直接说的项目经历,(哪年在哪个公司呆过) 2,问是否有带过团队的经历,我说去年带过一次. 3,Struts是单例模式还是多例模式?我先说单例模式,后说多例模式. Struts1是单例 ...