[HG]AK 题解
2024-10-07 08:56:33
前言
什么鬼畜玩意,扶我起来,我要用__int128,这辈子都不珂能用龟速乘的...
真香。
题解
我们知道这个模数是个神奇的东西
\(2305843008676823040 = 2^{29} \times 3 \times 5 \times 17 \times 65537\)
所以\(\varphi(2305843008676823040) = 2^{28} \times 2 \times 2^2 \times 2^4 \times 2^{16} = 2^{51}\)
\(a^{\varphi(n)}=1\ (mod\ n)\),所以在\(60\)次以内,该数\(mod 2305843008676823040\)一定会变为\(1\)。
用线段树维护序列,再加一个龟速乘,解决问题
#include <cstdio>
#define ll long long
const ll MOD = 2305843008676823040ll;
ll s[1000001];
bool flg[1000001];
long long read(){
long long x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}
inline ll mul(ll x, ll y){
ll res = 0;
for ( ; y; y >>= 1ll, (x <<= 1ll) %= MOD)
if(y & 1)
(res += x) %= MOD;
return res;
}
void build(int pos, int l, int r){
if (l == r){
s[pos] = read();
flg[pos] = ((s[pos] == 1 || !s[pos]) ? 1 : 0);
return ;
}
int mid = (l + r) >> 1;
build(pos << 1, l, mid);
build(pos << 1 | 1, mid + 1, r);
flg[pos] = flg[pos << 1] & flg[pos << 1 | 1];
s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % MOD;
}
ll query(int pos, int l, int r, int x, int y){
if (x <= l && r <= y && flg[pos])
return s[pos];
if (l == r){
ll res = s[pos];
ll tmp = mul(s[pos], s[pos]);
if (tmp == s[pos])
flg[pos] = 1;
s[pos] = tmp;
return res;
}
int mid = (l + r) >> 1;
ll res;
if (y <= mid)
res = query(pos << 1, l, mid, x, y);
else if (x > mid)
res = query(pos << 1 | 1, mid + 1, r, x, y);
else{
res = query(pos << 1, l, mid, x, y);
(res += query(pos << 1 | 1, mid + 1, r, x, y)) %= MOD;
}
s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % MOD;
flg[pos] = flg[pos << 1] & flg[pos << 1 | 1];
return res
}
int main(){
int n = read(), m = read();
for (register int i = 1; i <= 1000000; ++i) flg[i] = 1;
build(1, 1, n);
while (m--){
int l = read(), r = read();
ll res = query(1, 1, n, l, r) % MOD;
printf("%lld\n", res);
}
return 0;
}
最新文章
- [PHP] 自定义错误处理
- ARC指南1 - strong和weak指针
- 史上最用心的iOS App上架流程【转】
- hdu 4405Aeroplane chess(概率DP)
- 怎么解决/bin/sh: arm-linux-gcc: not found make
- 1990-D. 幻方
- 关于Azure存储账户中存储虚拟机VHD文件的注意事项
- java中创建对象 类名 对象名=new 类名(); 后面的()什么意思
- [译]C++如何切分字符串
- 动态修改UINavigationBar的背景色
- H264 编码详解
- 九度OJ 题目1371:最小的K个数
- Android_使用getIdentifier()获取资源Id
- C++ 版本的split_string
- Gate One
- MFC: 获得关机消息;阻止Windows关机
- 【CSS3】transition过渡和animation动画
- Eclipse使用Maven2的一次环境清理记录
- python之文件目录操作
- HeadFIrst Ruby 第二章总结 methods and classes
热门文章
- pycharm运行测试用例遇到错误:ZeroDivisionError: float division by zero的原因
- 1.parrot os 3.5-----nmap-----katoolin--zenmap
- 浅谈vue学习之组件通信
- Linux 后台执行python或者java代码的命令
- js Functor Copy
- MySQL-快速入门(4)MySQL函数
- JS中创建10个a标签,点击弹出对应的序号
- [2019上海网络赛J题]Stone game
- activemq热备与消息丢失
- linux(centeros)svn的安装