1. 程式人生 > >數論板子——來自Loi_black

數論板子——來自Loi_black

rime str n! std size ems pan log turn

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> 
using namespace std;
const int maxn=200005;
int prime[maxn];
bool not_prime[maxn];
int main()
{
    int n,cnt=0;
    scanf("%d",&n);
    memset(not_prime,0,sizeof(not_prime));
    not_prime[1]=true;
    for
(int i=2;i<=n;i++) { if(!not_prime[i]) prime[++cnt]=i; for(int j=1;j<=cnt;j++) { if(prime[j]*i>n) break; not_prime[prime[j]*i]=true; if(i%prime[j]==0) break; } } for(int i=1;i<=cnt;i++) printf(
"%d ",prime[i]); return 0; } //線性篩
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int x,y;
int exgcd(int a,int b)
{
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    else
    {
        int t;
        int d=exgcd(b,a%b);
        t
=x; x=y; y=t-a/b*x; return d; } } int main() { int a,b; scanf("%d%d",&a,&b); exgcd(a,b); // cout<<__gcd(a,b)<<endl; cout<<x<<" "<<y<<endl; return 0; } //擴展歐幾裏得
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int gcd(int a,int b)
{
    if(!b) return a;
    else return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    cout<<gcd(a,b)<<" "<<lcm(a,b)<<endl;
    return 0;
}

//gcd+lcm
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
    long long n;
    scanf("%lld",&n);
    for(long long i=2;i<=n;i++)
    {
        while(n!=i)
        {
            if(n%i==0)
            {
                printf("%lld*",i);
                n=n/i;              
            }
            else break;
        }
    }
    printf("%lld",n);
    return 0;
} 
//分解質因數
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int a[233],mo[233];
int gcd(int a,int b)
{
    if(!b) return a;
    else return gcd(b,a%b);
} 
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i],&mo[i]);
    int ans=0,nowmo=1;
    for(int i=1;i<=n;i++)
    {
        int other=a[i],othermo=mo[i];
        if(othermo>nowmo)
        {
            swap(ans,other);
            swap(nowmo,othermo);
        }
        while(ans%othermo!=other)
            ans+=nowmo;
        nowmo=lcm(nowmo,othermo); 
    }
    printf("%d",ans);
}
//大數翻倍法

數論板子——來自Loi_black