5 solutions

  • 4
    @ 2024-1-28 15:25:20

    题意:

    求(b^p)%k的值

    代码:

    #include<iostream>
    #define int long long
    using namespace std;
    int b,p,k;
    int f(int x,int y)//底数与指数
    {
    	if(y==0)//边界
    	{
    		return 1;
    	}
    	int ans=f(x,y/2)%k;//快速幂性质
    	return ((y%2==1)?(((x*ans)%k*ans)%k):((ans*ans)%k));//指数是奇数还是偶数
    }
    signed main()//因为用了 #define
    {
    	cin>>b>>p>>k;//输入
    	cout<<b<<"^"<<p<<" mod "<<k<<"="<<f(b,p);//输出
    	return 0;//完结散花
    }
    
    • 0
      @ 2024-1-28 15:24:17

      AC呆马

      #include<bits/stdc++.h>
      using namespace std;
      long long b,p,k;
      long long f(int x,int y){
          if(y==0) return 1;
          long long ans=f(x,y/2)%k;
          return y&1?(ans*ans%k*x):(ans*ans%k);
      }
      int main(){
          cin>>b>>p>>k;
          printf("%lld^%lld mod %lld=%lld",b,p,k,f(b,p)%k);
          return 0;
      }
      
      • -2
        @ 2024-1-25 9:34:44
        #include<bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        ll binpow(ll a,ll b,ll k){
        	ll res=1;
        	while(b>0){
        		if(b&1)res=res*a%k;
        		a=a*a%k;
        		b>>=1;
        	}
        	return res;
        }
        int main(){
        	ll b,p,k;
        	cin>>b>>p>>k;
        	cout<<b<<'^'<<p<<' '<<"mod"<<' '<<k<<'='<<binpow(b,p,k);
        	return 0;
        }
        
        • -3
          @ 2024-1-25 9:42:39
          #include<bits/stdc++.h>
          
          using namespace std;
          
          typedef long long fw;
          
          fw binpow(fw a, fw b, fw k){
          	fw res = 1;
          	while(b > 0){
          		if(b&1){
          			res = res * a % k;
          		}
          		a = a * a % k;
          		b >>= 1;
          	}
          	return res;
          }
          
          int main(){
          	fw a, b, k;
          	cin >> a >> b >> k;
          	cout << a << "^" << b << " mod " << k << "=" << binpow(a, b, k);
          	return 0;
          }
          
          • -3
            @ 2024-1-25 9:35:24
            #include<bits/stdc++.h>
            using namespace std;
            long long b,p,k;
            long long pw(long long a,long long b,long long t){
            	long long k=1;
            	while(b>0){
            		if(b&1) k=k*a%t;
            		a=a*a%t;
            		b>>=1;
            	}
            	return k;
            }
            int main(){
            	cin>>b>>p>>k;
            	cout<<b<<"^"<<p<<" mod "<<k<<'='<<pw(b,p,k)<<"\n";
            	return 0;
            }
            
            • 1

            Information

            ID
            811
            Time
            1000ms
            Memory
            256MiB
            Difficulty
            7
            Tags
            # Submissions
            246
            Accepted
            52
            Uploaded By