快速幂、欧几里得算法

/*///暴力枚举求素数

for(int i=2;i<=n;i++)
{
    bool g=0;
    for(int j=2;j*j<=i;j++)
    if(i%j==0)
    {
        g=1;
        break;
    }
    if(g==0)
    {
        tot++;
        p[tot]=i;
    }
}
///普通筛法

for(int i=2;i<=n;i++)
    if(prime[i]==0)
{
    p[++tot]=i;
    for(int j=2;j*i<=n;j++)
        prime[j*i]=1;
}
///线性筛法 将每一个合数
for(int i=2;i<=n;i++)
{
    if(prime[i]==0)
        p[++tot]=i;
    for(int j=1;j<=tot && i*p[j]<=n;j++)
    {
        prime[i*p[j]] = 1;
        if(i%p[j]==0)
            break;
    }
}

///快速幂 a^b

///逆元
///gcd
int gcd (int a,int b)
{
    if(b==0) return a;
    else
        return gcd(b,a%b);

}
///拓展欧几里得
int ex_gcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int r=ex_gcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-(a/b)*y;
    return y;
}
*/
/*
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    int t;
    LL m,n;
    cin>>t;
    while(t--)
    {
        scanf("%lld %lld",&m,&n);
        if(m==1 || n==1)
        {
            cout<<"NO"<<endl;
            continue;
        }
        if((m%2==0 && n%2==0) || (__gcd(m,n)==m || __gcd(m,n)==n))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}
*/
/*
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int p,q;

    while(cin>>p>>q)
    cout<<p+q-__gcd(p,q)<<endl;
}
*/
/*
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int p,q;
    while(cin>>p>>q)
    cout<<p*q/__gcd(p,q)<<endl;
}

*/

/*
///快速幂模板  a^b%c

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;

LL mod (LL a,LL b,LL c)
{
    LL ans =1;
    a=a%c;
    while(b>0)
    {
        if((b&1)==1)
            ans=(ans*a)%c;
        b>>=1;
        a=(a*a)%c;
    }
    return ans;
}

bool isprime(LL x)
{
    LL i,j,k=(int)sqrt(x);
    if((x==2)||(x==3))
        return 1;
    if(x==0)
        return 0;
    for(i=2;i<=k;i++)
        if(x%i==0)
        return 0;
    return 1;
}
int main()
{
    LL p,m,result;
    while(scanf("%lld %lld",&p,&m))
    {
        if((p==0) && (m==0))
            break;
        if(isprime(p))
            cout<<"no"<<endl;
        else
        {
            if(mod(m,p,p)==m)
                cout<<"yes"<<endl;
            else
                cout<<"no"<<endl;
        }
    }
    return 0;
}

*/
/*
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
int main()
{
    LL M,N,i,n,a,b,c,sum;
    scanf("%lld",&N);
    while(N--)
    {
        scanf("%lld%lld",&M,&n);
        sum=0;
        for(i=0;i<n;i++)
        {
            c=1;
            scanf("%lld%lld",&a,&b);
            while(b)
            {
                if(b&1)
                    c=c*a%M;
                a=a*a%M;
                b/=2;
            }
            sum=sum+c%M;
        }
        printf("%lld",sum%M);
    }
}

*/

///a^b%c
/*
LL mod (LL a, LL b, LL c)
{
    LL ans =1;
    a=a%c;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%c;
        b>>=1; ///等价于b/2
        a=(a*a)%c;
    }
    return ans;
}
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL mod (LL a,LL b,LL c)
{
    LL ans =1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%c;
        b>>=1;
        a=(a*a)%c;
    }
    return ans;
}
int main()
{
    int a,b;
    while(scanf("%d %d",&a,&b) , (a || b))
    {
        printf("lld\n",mod(a,b,1000));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值