紫薯 第十章

 1.欧几里得算法和唯一分解定理

#include<iostream>  
#include<algorithm>  
#include<cstring>  
#include<cstdio>  
using namespace std;  
int a[1000005];  
int GCD(int a,int b)  
{  
    if(a<b)swap(a,b);  
    return b==0?a:GCD(b,a%b);  
}  
int judge(int m)  
{  
    a[1]/=GCD(a[1],a[0]);  
    for(int i=2;i<m;++i)  
    {  
        a[1]/=GCD(a[i],a[1]);  
         if(a[1]==1)  
            return 1;  
    }  
    return 0;  
}  
int main()  
{  
    int n,m,i;  
    char str[1000005];  
    scanf("%d",&n);  
    while(n--)  
    {  
        memset(a,0,sizeof(a));  
        scanf("%s",&str);  
        for (i=0,m=0;str[i]!='\0';++i)  
        {  
            if(str[i]>='0'&&str[i]<='9')  
                a[m]=a[m]*10+str[i]-'0';  
            else  
                m++;  
        }  
        if(judge(m+1))  
        {  
            printf("YES\n");  
        }  
        else  
        {  
            printf("NO\n");  
        }  
    }  
    return 0;  
}  
除法表达式

 

 2.埃筛

#include<stdio.h>
#include<string.h>
#include<math.h>
bool visited[10000005];
int prime[500000];
int main(){
    int n, m;
    scanf("%d", &n, &m);
    int i, j, c = 0;
    int r = sqrt(n + 0.5);
    memset(visited, 0, sizeof(visited));
    for (i = 2; i <= r; i++){
        if (!visited[i]){
            prime[c++] = i;
            for (j = i * i; j <= n; j += i)
                visited[j] = 1;
        }
    }
    memset(visited, 0, sizeof(visited));
    for (i = 0; i < c; i++){
        for (j = prime[i] * prime[i]; j <= n; j += prime[i] * prime[i])
            visited[j] = 1;
    }
    for (i = m; i <= n; i++){
        if (!visited[i])
            printf("%d ", i);
    }

    return 0;
}
无平方因子数

 

3.拓展欧几里得算法

#include<cstdio>
using namespace std;
const int INF = 1000; 
void gcd(int a, int b, int &d, int &x, int &y)
{
    if(!b)
    {
        d = a; x = 1; y = 0;
    }else{
        gcd(b, a%b, d, y, x);
        y -= x*(a/b);
    }
}

int main()
{
    int a,b,c,d;
    scanf("%d%d%d",&a,&b,&c);         
    int x1,x2,y1,y2;
    scanf("%d%d%d%d",&x1,&x2,&y1,&y2);//整点的范围 

    int x0,y0;                       //ax+by=gcd(a,b)的一个解 
    gcd(a,b,d,x0,y0); 
    int a2 = a/d, b2 = b/d;         //其他解 
    int count = 0; 

    if(c%d==0)
    {
        for(int k=0; k<INF; k++)
        {
            x0 = x0+k*b2;
            y0 = y0-k*a2;
            if(x0<x1||x0>x2||y0<y1||y0>y2)
            continue;
            count++;
        }
    }
    printf("%d\n",count);
    return 0;
}
直线上的点

 

posted @ 2018-04-07 00:54  Roni_i  阅读(138)  评论(0编辑  收藏  举报