题面

传送门

题解

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(分块)的更多相关文章

  1. HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面. Solution ...

  2. 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里是不变的,可以数论分块,再在每一段里 ...

  3. 杭电多校第七场 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 ...

  4. [LOJ#162]模板题-快速幂2

    <题目链接> 注意:这可能也是一道模板题. 注意2:$p=998224352$ 注意3:对于$100\%$的数据,$n\leq 5 \times 10^6$ 这个题很启发思路,如果直接快速 ...

  5. hdu6395 (矩阵快速幂+分块)

    Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...

  6. hdu 6395Sequence【矩阵快速幂】【分块】

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  7. HDU-6395 多校7 Sequence(除法分块+矩阵快速幂)

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. [hdu-6395]Sequence 分块+矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时  加的下取整的数随 ...

  9. HDU - 6395 Sequence (整除分块+矩阵快速幂)

    定义数列: $\left\{\begin{eqnarray*} F_1 &=& A \\ F_2 &=& B \\ F_n &=& C\cdot{}F_ ...

随机推荐

  1. eclipse报Access restriction: The type 'BASE64Decoder' is not API处理方法

    今天从svn更新代码之后,由于代码中使用了BASE64Encoder  更新之后报如下错误: Access restriction: The type ‘BASE64Decoder’ is not A ...

  2. 新手C#类、对象、字段、方法的学习2018.08.05

    类:具有相似属性和方法的对象的集合,如“人”是个类. 对象(实例):对象是具体的看得见摸得着的,如“张三”是“人”这个类的对象.(new Person()开辟了堆空间中,=开辟了栈空间,变量P存放在该 ...

  3. Linux安装centos7

    安装 选择安装centos7,按回车 进入到安装界面: 选择我要自定义分区,然后点击左上角done: 然后自定义分区(swap分区一般为内存的2倍,我这里用的虚拟机截的图,所以内存给的少,具体按照自己 ...

  4. Socket、RPC通信实例,简单版本,仅供查阅

    TCP/IP Socket 如果使用TCP协议来传递数据,客户端和服务器端需要分别经过以下步骤: server: 创建socket对象 - bind(绑定socket到指定地址和端口) - liste ...

  5. datagridview paging

    http://www.codeproject.com/Articles/211551/A-Simple-way-for-Paging-in-DataGridView-in-WinForm

  6. [Selenium] 最大化或自定义浏览器的大小

      driver.manage().window().maximize(); //将浏览器设置为最大化的状态   driver.manage().window().setSize(new Dimens ...

  7. Solr分词搜索结果不准确

    Solr的schema.xml默认配置分词后条件取 OR 例如:大众1.6T  系统会自动分词为  [大众] [1.6T](ps:不同分词器分词效果不同)   会搜索出包含 [大众 OR  1.6T] ...

  8. span和input同一行布局的时候,高度偏移解决方案

    input标签或收盘标签 添加代码: vertical-align:top;

  9. HDU 6065 RXD, tree and sequence (LCA+DP)

    题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深 ...

  10. Linux的进程与服务(二)

    1.查看进程 ps - processes snapshot ps aue ps -elf [root@localhost ~]# ps aue USER PID %CPU %MEM VSZ RSS ...