跳到正文

题解

P1226的n种做法

Zhu_tian_yu约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年1月22日
更新
2026年1月23日
篇幅
约 9 分钟
题目
P1226

版本

  1. v3当前
  2. v2
  3. v1

二分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    long long m=pow(x,y/2)%p,n;
    n=(m%p*m%p)%p;
    if(y%2>0){
        n=(n%p*pow(x,y%2)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

三分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    if(y==2)return (x%p*x%p)%p;
    long long m=pow(x,y/3)%p,n;
    n=(m%p*m%p*m%p)%p;
    if(y%3>0){
        n=(n%p*pow(x,y%3)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

四分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    if(y==2)return (x%p*x%p)%p;
    if(y==3)return (x%p*x%p*x%p)%p;
    long long m=pow(x,y/4)%p,n;
    n=(m%p*m%p*m%p*m%p)%p;
    if(y%4>0){
        n=(n%p*pow(x,y%4)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

五分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    if(y==2)return (x%p*x%p)%p;
    if(y==3)return (x%p*x%p*x%p)%p;
    if(y==4)return (x%p*x%p*x%p*x%p)%p;
    long long m=pow(x,y/5)%p,n;
    n=(m%p*m%p*m%p*m%p*m%p)%p;
    if(y%5>0){
        n=(n%p*pow(x,y%5)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

六分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    if(y==2)return (x%p*x%p)%p;
    if(y==3)return (x%p*x%p*x%p)%p;
    if(y==4)return (x%p*x%p*x%p*x%p)%p;
    if(y==5)return (x%p*x%p*x%p*x%p*x%p)%p;
    long long m=pow(x,y/6)%p,n;
    n=(m%p*m%p*m%p*m%p*m%p*m%p)%p;
    if(y%6>0){
        n=(n%p*pow(x,y%6)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

七分

C++
#include<bits/stdc++.h>
#define please return
#define AC 0
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
#define print cout<<
#define elif else if
#define cf(i) cout<<fixed<<setprecision(i)
using namespace std;
long long a,b,p,s=1;
long long pow(long long x,long long y){
    if(y==1)return x;
    if(y==0)return 1;
    if(y==2)return (x%p*x%p)%p;
    if(y==3)return (x%p*x%p*x%p)%p;
    if(y==4)return (x%p*x%p*x%p*x%p)%p;
    if(y==5)return (x%p*x%p*x%p*x%p*x%p)%p;
    if(y==6)return (x%p*x%p*x%p*x%p*x%p*x%p)%p;
    long long m=pow(x,y/7)%p,n;
    n=(m%p*m%p*m%p*m%p*m%p*m%p*m%p)%p;
    if(y%7>0){
        n=(n%p*pow(x,y%7)%p)%p;
    }
    n%=p;
    return n;
}
int main(){
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    cin>>a>>b>>p;
    s=pow(a,b);
    s%=p;
    printf("%lld^%lld mod %lld=%lld",a,b,p,s);
    please AC;
}

AC记录

原文