数论基础2

素数筛

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e6+5;
const int maxn2=1e7+5;
int prime[maxn],pprime[maxn],cnt,tot;
bool check[maxn2];
void Init()
{
    cnt=1;
    memset(check,0,sizeof(check));
    for(int i=2;i<=maxn2;i++)
    {
       if(!check[i])
        prime[cnt++]=i;
       for(int j=1;j<=cnt&&i*prime[j]<=maxn2;j++)
       {
           check[i*prime[j]]=true;
           if(i%prime[j]==0)
            break;
       }
    }
}
void Init2()
{
    tot=1;
    for(int i=1;prime[prime[i]]<=1e7+5;i++)
        pprime[i]=prime[prime[i]],tot++;
    //cout<<tot<<endl;
}
int main()
{
    int n;
    Init();
    Init2();
    //cout<<cnt<<endl;
    //for(int i=0;i<tot;i++)
        //cout<<pprime[i]<<endl;
    while(cin>>n)
    for(int i=1;i<=tot;i++)
        if(pprime[i]>=n)
        {
            cout<<pprime[i]<<endl;
            break;
        }
    return 0;
}

扩展欧几里得求逆元

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256

#include <iostream>

using namespace std;

int Exgcd(int a, int b, int& x, int& y)
{
    if(b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    int d = Exgcd(b, a%b, x, y);
    int temp = x;
    x = y;
    y = temp - a/b*y;
    return d;
}
int main()
{
    int n,m,x,y,d;
    while(cin>>m>>n)///m*x-n*y=1
    {
        int d=Exgcd(m,n,x,y);
        y=-(m*x-1)/n;
        while(x<0)
        {   
            x=x+n/d;
        }
        cout<<x<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值