zoukankan      html  css  js  c++  java
  • LOJ. 162. 快速幂2(快速幂)

    题目链接


    (Description)
    给定(x,T)(T)次询问,每次给定(k)(x^k\% 998244352)
    (x,klt 998244352, Tleq 5 imes10^6)

    (Solution)
    做法1:
    直接快速幂就行了。没过的是(5e6)还没用快读的(所以时限应该改0.5s)。

    做法2:
    常规以(2)为底的快速幂:(x^n=x^{lfloorfrac{n}{2} floor imes2} imes x^{n\%2})
    同理可以改成以(3)为底:(x^n=x^{lfloorfrac{n}{3} floor imes3} imes x^{n\%3})。(但是不能位运算优化,不如常规写法)

    int FP(LL x,int k)
    {
    	LL t=1;
    	for(; k; k/=3,x=x*x%mod*x%mod)
    		if(k%3==1) t=t*x%mod;
    		else if(k%3==2) t=t*x%mod*x%mod;
    	return t;
    }
    

    同理可以改成以任意数(k)为底:(x^n=x^{lfloorfrac{n}{k} floor imes k} imes x^{n\%k})
    (k=sqrt{mod}),则只需预处理(x^0,x^k,x^{2k},...,x^{lfloorfrac{mod}{k} floor imes k}),以及(x^0,x^1,...,x^{k-1}),复杂度(O(k))
    询问就是(O(1))的了。

    细节:最好取(k=sqrt{mod}+1)?应该问题不大但是基本都加了(1)。。


    //308ms	1.0Mb
    #include <bits/stdc++.h>
    #define pc putchar
    #define MAXIN 300000
    #define gc() getchar()
    //#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
    #define pb emplace_back
    #define mod 998244352
    typedef long long LL;
    const int N=1e5+5,K=(int)(sqrt(mod))+1;
    
    int A[N],B[N];
    char IN[MAXIN],*SS=IN,*TT=IN;
    
    inline int read()
    {
    	int now=0,f=1; char c=gc();
    	for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
    	for(;isdigit(c);now=now*10+c-48,c=gc());
    	return now*f;
    }
    int FP(LL x,int k)
    {
    	LL t=1;
    	for(; k; k>>=1,x=x*x%mod)
    		if(k&1) t=t*x%mod;
    	return t;
    //	LL t=1;
    //	for(; k; k/=3,x=x*x%mod*x%mod)
    //		if(k%3==1) t=t*x%mod;
    //		else if(k%3==2) t=t*x%mod*x%mod;
    //	return t;
    }
    
    int main()
    {
    	LL x=read(),xk=FP(x,K);
    	A[0]=B[0]=1;
    	for(int i=1,cnt=mod/K; i<=cnt; ++i) A[i]=A[i-1]*xk%mod;
    	for(int i=1; i<K; ++i) B[i]=B[i-1]*x%mod;
    
    	for(int T=read(),n; T--; )
    		n=read(), printf("%d ",int(1ll*A[n/K]*B[n%K]%mod));
    
    	return 0;
    }
    
    ------------------------------------------------------------------------------------------------------------------------
    无心插柳柳成荫才是美丽
    有哪种美好会来自于刻意
    这一生波澜壮阔或是不惊都没问题
    只愿你能够拥抱那种美丽
    ------------------------------------------------------------------------------------------------------------------------
  • 相关阅读:
    知识加油站
    markdown 使用
    今日计划
    继续继续
    我回来了
    2020/5/8
    2020/5/10
    明天一定要学习了
    入驻博客园
    this is a test
  • 原文地址:https://www.cnblogs.com/SovietPower/p/14491575.html
Copyright © 2011-2022 走看看