• hdu1061+hdu2817快速幂



    更新:calculate(a*b)modp通过乘法会溢出,所以通过加法加上long long 实现。

    代码如下:

    #include<iostream>
    using namespace std;
    typedef long long ll ;
    ll power(ll a,ll b,ll p){
        a%=p,b%=p;
        ll ans=0;
        while(b){
            if(b&1) ans=(ans+a)%p;//ans+a最多只会是2*(le18),不会超过ll的范围 
            a=(a*2)%p;
            b>>=1;
        }
        return ans;
    }
    int main(){
        ll a,b,p;
        cin>>a>>b>>p;
        cout<<power(a,b,p)<<endl;
    } 

     

    代码如下

    hdu1061:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef unsigned int ui;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 #define pf printf
     7 #define mem(a,b) memset(a,b,sizeof(a))
     8 #define prime1 1e9+7
     9 #define prime2 1e9+9
    10 #define pi 3.14159265
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define scand(x) scanf("%llf",&x) 
    14 #define f(i,a,b) for(int i=a;i<=b;i++)
    15 #define scan(a) scanf("%d",&a)
    16 #define mp(a,b) make_pair((a),(b))
    17 #define P pair<int,int>
    18 #define dbg(args) cout<<#args<<":"<<args<<endl;
    19 #define inf 0x7ffffff
    20 inline int read(){
    21     int ans=0,w=1;
    22     char ch=getchar();
    23     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    24     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    25     return ans*w;
    26 }
    27 int n,m,t;
    28 const int maxn=1e5+10;
    29 const int mod=10;
    30 int pow(int n,int k)
    31 {
    32     int ans=1;
    33     n%=10;
    34     while(k)
    35     {
    36         if(k&1)ans=(ans*n)%mod;
    37         k>>=1;
    38         n=(n*n)%mod;
    39     }
    40     return ans;
    41 }
    42 int main()
    43 {
    44     //freopen("input.txt","r",stdin);
    45     //freopen("output.txt","w",stdout);
    46     std::ios::sync_with_stdio(false);
    47     t=read();
    48     while(t--)
    49     {
    50         n=read();
    51         pf("%d
    ",pow(n,n));
    52     }
    53 } 

    hdu2817:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef unsigned int ui;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 #define pf printf
     7 #define mem(a,b) memset(a,b,sizeof(a))
     8 #define prime1 1e9+7
     9 #define prime2 1e9+9
    10 #define pi 3.14159265
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define scand(x) scanf("%llf",&x) 
    14 #define f(i,a,b) for(int i=a;i<=b;i++)
    15 #define scan(a) scanf("%d",&a)
    16 #define mp(a,b) make_pair((a),(b))
    17 #define P pair<int,int>
    18 #define dbg(args) cout<<#args<<":"<<args<<endl;
    19 #define inf 0x7ffffff
    20 inline int read(){
    21     int ans=0,w=1;
    22     char ch=getchar();
    23     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    24     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    25     return ans*w;
    26 }
    27 int n,m,t;
    28 const int maxn=1e5+10;
    29 const ll mod=200907;
    30 ll pow(ll n,ll  k)
    31 {
    32     ll ans=1;
    33     while(k)
    34     {
    35         if(k&1)ans=(ans*n)%mod;
    36         k>>=1;
    37         n=(n*n)%mod;
    38     }
    39     return ans;
    40 }
    41 int main()
    42 {
    43     //freopen("input.txt","r",stdin);
    44     //freopen("output.txt","w",stdout);
    45     std::ios::sync_with_stdio(false);
    46     t=read();
    47     while(t--)
    48     {
    49         ll a,b,c,d;
    50         scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    51         if(2*b==(a+c))
    52         {
    53             pf("%lld
    ",(a%mod+(d-1)*(b-a)%mod)%mod);
    54         }
    55         else 
    56         {
    57             ll q=b/a;
    58             pf("%lld
    ",(1ll*a*pow(q,d-1))%mod);
    59         }
    60     }    
    61 } 
  • 相关阅读:
    Miller-Rabin算法
    拟阵
    第一次作业
    实验四 201771010101 白玛次仁
    201771010101 白玛次仁
    201771010101 白玛次仁 《2018面向对象程序设计(Java)课程学习进度条》
    201771010101 白玛次仁
    201771010101 白玛次仁
    达拉草201771010105《面向对象程序设计(java)》第二周学习总结
    达拉草201771010105《面向对象程序设计(java)》第一周学习总结
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12724924.html
Copyright © 2020-2023  润新知