素数筛
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;
}