LOJ#162. 快速幂 2(分块)
题面
题解
orzljz
我们分块,设\(s=\sqrt{p}+1\),那么\(x^a\)可以拆成\((x^s)^{a/s}\)和\(x^{a\bmod s}\),\(O(s)\)预处理,\(O(1)\)计算就可以了
//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]=' ';
}
const int N=50005,P=998244352;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
return res;
}
int bin[N],bs[N],n,x,s,a;
int main(){
// freopen("testdata.in","r",stdin);
x=read(),n=read(),s=sqrt(P)+1;
bin[0]=1;fp(i,1,s)bin[i]=mul(bin[i-1],x);
bs[0]=1;fp(i,1,s)bs[i]=mul(bs[i-1],bin[s]);
while(n--)a=read(),print(mul(bs[a/s],bin[a%s]));
return Ot(),0;
}
LOJ#162. 快速幂 2(分块)的更多相关文章
- HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- HDU6395 Sequence(矩阵快速幂+数论分块)
题意: F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n 给定ABCDPn,求F(n) mod 1e9+7 思路: P/n在一段n里是不变的,可以数论分块,再在每一段里 ...
- 杭电多校第七场 1010 Sequence(除法分块+矩阵快速幂)
Sequence Problem Description Let us define a sequence as below f1=A f2=B fn=C*fn-2+D*fn-1+[p/n] Your ...
- [LOJ#162]模板题-快速幂2
<题目链接> 注意:这可能也是一道模板题. 注意2:$p=998224352$ 注意3:对于$100\%$的数据,$n\leq 5 \times 10^6$ 这个题很启发思路,如果直接快速 ...
- hdu6395 (矩阵快速幂+分块)
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...
- hdu 6395Sequence【矩阵快速幂】【分块】
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU-6395 多校7 Sequence(除法分块+矩阵快速幂)
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [hdu-6395]Sequence 分块+矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时 加的下取整的数随 ...
- HDU - 6395 Sequence (整除分块+矩阵快速幂)
定义数列: $\left\{\begin{eqnarray*} F_1 &=& A \\ F_2 &=& B \\ F_n &=& C\cdot{}F_ ...
随机推荐
- js练习 closure
window.onload = function() { for (var i = 1; i < 4; i++) { var id = doc ...
- LevelDB 读取记录
[LevelDB 读取记录] LevelDb是针对大规模Key/Value数据的单机存储库,从应用的角度来看,LevelDb就是一个存储工具.而作为称职的存储工具,常见的调用接口无非是新增KV,删除K ...
- Shiro框架的简单应用
一.概念 Shiro是一个安全框架,可以进行角色.权限管理. Shiro主要功能如下:Authentication(认证):用户身份识别,通常被称为用户“登录”Authorization(授权):访问 ...
- [leetcode]141. Linked List Cycle判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- VGGNet
VGGNet 是牛津大学计算机视觉组(Visual Geometry Group)和 GoogleDeepMind 公司的研究员一起研发的的深度卷积神经网络. 在ImageNet大型视觉识别挑战 IL ...
- Laravel 使用 Provider 为程序提供运行时配置服务
需求: 配置参数存在数据库中,Model 是 aah,需要在每次运行时,程序可以在任何地方采用 config("aah.name") 的方式访问配置信息. 思路: 采用 Provi ...
- DB2存储过程通过游标实现批量数据处理
CREATE procedure proc_change()LANGUAGE SQLBEGIN DECLARE l_id INTEGER; DECLARE l_detail_id INTEGER; D ...
- [C#] Delegate, Multicase delegate, Event
声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...
- Linux执行YUM命令报错解决方案
Loaded plugins: rhnplugin, security This system is not registered with RHN. RHN support will be disa ...
- PHP5.2 $arr = [] 初始化数组出现问题
初始化数组 $arr=[] ,出现问题,使用 $arr = array() ,一切正常