28 条题解

  • 0
    @ 2022-10-23 11:23:50

    使用分治思想,每次将指数 nn 减半,再平方。但如果 nn 是技术,则要再乘 aa

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int INF=0x3f3f3f3f;
    inline ll read()
    {
    	ll x=0,f=1;char ch=getchar();
    	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    	return x*f;
    }
    ll quickpow(ll a,ll b,ll m){
    	if(b==0)  return 1;
    	ll tmp=quickpow(a,b/2,m)%m;
    	ll ans=tmp*tmp%m;
    	if(b&1)  ans=ans*a%m;
    	return ans; 
    }
    int main(){
    	ll a,b,p;
    	a=read(),b=read(),p=read();
    	cout<<quickpow(a,b,p);
    	return 0;
    }
    
    
    • 0
      @ 2022-8-22 20:14:26

      学完初一数学自然就会了。

      #include<bits/stdc++.h>
      #define ll long long
      using namespace std;
      long long a,b,s,ans;
      ll p(ll a,ll k)
      {
      	ll res=1; 
      	while(k)
      	{
      		if(k&1) 
      		{
      		   res=(ll)res*a%s;
      		}
      		k>>=1;
      		a=(ll)a*a%s; 
      	}
      	return res;
      }
      int main()
      {
      	cin>>a>>b>>s;
      	cout<<p(a,b)%s;
      }
      
      • 0
        @ 2022-5-29 15:53:04
        a=input().split#输入,切片
        print(str(pow(int(a[0]),int(a[1]),int(a[2]))))#pow函数
        
        • -1
          @ 2025-1-15 15:22:47

          注释了啊

          我第一次在这里做的

          #include <bits/stdc++.h>
          using namespace std;
          /*
          (a+b)%k = (a%k+b%k)%k
          (a*b)%k = ((a%k)*(b%k))%k 
          */
          
          //快速幂  (a^x)%p
          // a=1231321313
          // x=5464646546
          // p=12313 
          
          long long a,x,p;
          
          long long f(int k){//a的k次方 %p的结果 
          	if(k==0) return 1;
          	if(k==1) return a%p;
          	long long tmp=f(k/2);
          	if(k%2==0){
          		return tmp*tmp%p;
          	}else{
          		return (tmp*tmp%p)*a%p;
          	}
          }
          
          int main(){
          	cin>>a>>x>>p;
              cout<<f(x);
              return 0;
          }
          
          • -1
            @ 2024-10-24 11:16:59

            位运算 code

            #include<stdio.h>
            using namespace std;
            unsigned long long a,b,mod;
            unsigned long long pow(unsigned long long a,unsigned long long b){
                unsigned long long ret = 1;
                while(b){
                    if(b&1)
                        ret=(ret*a)%mod;
                    a=(a*a)%mod;
                    b>>=1;
                }
                return ret;
            }
            int main(){
                scanf("%lld%lld%lld",&a,&b,&mod);
                printf("%lld",pow(a,b)%mod);
            return 0;
            }
            
            • -1
              @ 2024-10-23 19:55:33
              #include<bits/stdc++.h>
              #define int long long
              using namespace std;
              int a,b,p;
              int qp(int a,int b,int p)
              {
              	int res=1;
              	while(b)
              	{
              		if(b&1)
              		{
              			res*=a;
              			res%=p;
              		}
              		a*=a;
              		a%=p;
              		b>>=1;
              	}
              	return res;
              }
              signed main()
              {
              	ios::sync_with_stdio(false);
              	cin.tie(0);
              	cout.tie(0);
              	cin>>a>>b>>p;
              	cout<<qp(a,b,p);
              	cout.flush();
              	return 0;
              }
              

              板子,就不说了,直接背

              • -1
                @ 2022-3-16 13:34:42

                python代码yyds(什

                a,b,c=input().split()

                a=int(a)

                b=int(b)

                c=int(c)

                print(pow(a,b,c))

                • -8
                  @ 2021-11-6 16:49:03

                  #include<bits/stdc++.h>

                  using namespace std;

                  int main()

                  {

                  unsigned long long a,b,mode ;
                  
                  cin>>a>>b>>mode;
                  
                  int sum=1;
                  
                  a =a%mode;
                  
                  while(b>0)
                  
                  {
                  
                      if (b%2==1)
                  
                  	{
                  
                  		sum =(sum*a)%mode;
                  
                  	}
                  
                      b /= 2;
                  
                      a = (a * a) % mode;
                  

                  }

                  cout<<sum;

                  }

                  信息

                  ID
                  171
                  时间
                  1000ms
                  内存
                  256MiB
                  难度
                  2
                  标签
                  递交数
                  1240
                  已通过
                  394
                  上传者