Red Huang

Red Huang

uva 10329

大數加上篩法

篩法記錄每個數有的質因數,除掉這個質因數就會再找到新的數

然後新的數繼續除以他自己的質因數,直到為 1 就可以找到所有質因數了

//  
//        GGGGGGGGGGGGG        CCCCCCCCCCCCC               AAA  
//     GGG::::::::::::G     CCC::::::::::::C              A:::A  
//   GG:::::::::::::::G   CC:::::::::::::::C             A:::::A  
//  G:::::GGGGGGGG::::G  C:::::CCCCCCCC::::C            A:::::::A  
// G:::::G       GGGGGG C:::::C       CCCCCC           A:::::::::A  
//G:::::G              C:::::C                        A:::::A:::::A  
//G:::::G              C:::::C                       A:::::A A:::::A  
//G:::::G    GGGGGGGGGGC:::::C                      A:::::A   A:::::A  
//G:::::G    G::::::::GC:::::C                     A:::::A     A:::::A  
//G:::::G    GGGGG::::GC:::::C                    A:::::AAAAAAAAA:::::A  
//G:::::G        G::::GC:::::C                   A:::::::::::::::::::::A  
// G:::::G       G::::G C:::::C       CCCCCC    A:::::AAAAAAAAAAAAA:::::A  
//  G:::::GGGGGGGG::::G  C:::::CCCCCCCC::::C   A:::::A             A:::::A  
//   GG:::::::::::::::G   CC:::::::::::::::C  A:::::A               A:::::A  
//     GGG::::::GGG:::G     CCC::::::::::::C A:::::A                 A:::::A  
//        GGGGGG   GGGG        CCCCCCCCCCCCCAAAAAAA                   AAAAAAA  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <climits>  
#include <vector>  
#include <set>  
#include <map>  
#include <queue>  
#include <cctype>  
#include <utility>  
#include <ctime>  
using namespace std;  
#ifdef DEBUG  
#define VAR(a,b) decltype(b) a=(b)  
#define debug(...) printf("DEBUG: "),printf(\_\_VA\_ARGS\_\_)  
#define gettime() end\_time=clock();printf("now running time is %.7f\\n",(float)(end\_time - start\_time)/CLOCKS\_PER\_SEC);  
#else  
#define VAR(a,b) \_\_typeof(b) a=(b)  
#define debug(...)  
#define gettime()  
#endif  
typedef unsigned int uint;  
typedef long long int Int;  
typedef unsigned long long int UInt;  
#define Set(a,s) memset(a,s,sizeof(a))  
#define Write(w) freopen(w,"w",stdout)  
#define Read(r) freopen(r,"r",stdin)  
#define Pln() printf("\\n")  
#define I\_de(x,n)for(int i=0;i<n;i++)printf("%d ",x\[i\]);Pln()  
#define De(x)printf(#x"%d\\n",x)  
#define For(i,x)for(int i=0;i<x;i++)  
#define CON(x,y) x##y  
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\\  
    printf("%d ",dp\[htx\]\[hty\]);}Pln();}  
#define M 1000005  
#define PII pair<int,int\>  
#define PB push\_back  
#define oo INT\_MAX  
#define Set\_oo 0x3f  
#define FOR(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)  
#define eps 1e-6  
clock\_t start\_time=clock(), end\_time;  
bool xdy(double x,double y){return x>y+eps;}  
bool xddy(double x,double y){return x>y-eps;}  
bool xcy(double x,double y){return x<y-eps;}  
bool xcdy(double x,double y){return x<y+eps;}  
int min3(int x,int y,int z){  
    int tmp=min(x,y);  
    return min(tmp,z);  
}  
int max3(int x,int y,int z){  
    int tmp=max(x,y);  
    return max(tmp,z);  
}  
int n,m;  
int numer\[5005\];  
int dnumer\[5005\];  
int factor\[5005\]\[5005\];  
int prime\[5005\],pn;  
int seize\[5005\];  
void pre(){  
    seize\[1\]=1;  
    for(int i=2;i<5005;i++){  
        if(!seize\[i\]){  
            for(int j=i;j<5005;j+=i){  
                seize\[j\]=i;  
            }  
        }  
    }  
}  
struct bignum{  
    Int n\[30\];  
    static const int maxn=26;  
    static const Int base\=10000;  
    bool flag;  
    void clear(){  
        Set(n,0);  
        flag=false;  
    }  
    bool check(){  
        int i=maxn-1;  
        for(;i>0;i--)if(n\[i\])break;  
        if(i>=25){  
            puts("-1");  
            return false;  
        }  
        printf("%lld",n\[i--\]);  
        for(;i>=0;i--)printf("%04lld",n\[i\]);  
        Pln();  
        return true;  
    }  
    bignum(Int x){  
        Set(n,0);  
        flag=false;  
        n\[0\]=x%base;  
    }  
    bignum(){  
        flag=false;  
        Set(n,0);  
    }  
    bignum mul(Int x){  
        bignum ans;  
        Int c=0;  
        for(int i=0;i<maxn;i++){  
            Int tmp=n\[i\]\*x;  
            ans.n\[i\]=tmp;  
        }  
        for(int j=0;j<maxn;j++){  
            Int tmp=ans.n\[j\]+c;  
            ans.n\[j\]=tmp%base;  
            c=tmp/base;  
        }  
        int i=maxn-1;  
        for(;i>0;i--)if(ans.n\[i\])break;  
        if(i>=25){  
            ans.flag=true;  
        }  
        return ans;  
    }  
    bignum operator\*(bignum &d)const{  
        bignum ans;  
        Int c=0;  
        for(int i=0;i<maxn;i++){  
            if(n\[i\]==0)continue;  
            for(int j=0;j+i<maxn;j++){  
                ans.n\[i+j\]+=n\[i\]\*d.n\[j\];  
                c=ans.n\[i+j\]/base;  
                ans.n\[i+j\]%=base;  
                for(int k=i+j+1;k<maxn;k++){  
                    Int tmp=ans.n\[k\]+c;  
                    ans.n\[k\]=tmp%base;  
                    c=tmp/base;  
                }  
  
            }  
        }  
        int i=maxn-1;  
        for(;i>0;i--)if(ans.n\[i\])break;  
        if(i>=25){  
            ans.flag=true;  
        }  
  
  
        return ans;  
    }  
    bignum operator+(bignum &d)const{  
        bignum ans;  
        Int c=0;  
        for(int i=0;i<maxn;i++){  
            Int tmp=n\[i\]+d.n\[i\]+c;  
            ans.n\[i\]=tmp%base;  
            c=tmp/base;  
        }  
        int i=maxn-1;  
        for(;i>0;i--)if(ans.n\[i\])break;  
        if(i>=25){  
            ans.flag=true;  
        }  
        return ans;  
    }  
    bignum add(Int x){  
        bignum ans;  
        Int tmp=n\[0\]+x,c;  
        ans.n\[0\]=tmp%base;  
        c=tmp/base;  
        for(int i=1;i<maxn;i++){  
            tmp=n\[i\]+c;  
            ans.n\[i\]=tmp%base;  
            c=tmp/base;  
        }  
        return ans;  
    }  
};  
void demo(){  
    bignum a(10),b(4);  
    a=a\*a;  
    b=a\*b;  
    a.check();  
    b.check();  
}  
int main() {  
    ios\_base::sync\_with\_stdio(0);  
    pre();  
//    demo();  
    while(~scanf("%d%d",&n,&m)){  
        Set(numer,0);  
        for(int i=0;i<n;i++){  
            int c,r;  
            scanf("%d%d",&c,&r);  
            if(c<r)continue;  
            if(c-r<r)r=c-r;  
            for(int j=c,k=1;k<=r;j--,k++){  
                int tmp=j;  
                while(tmp!=1){  
                    numer\[seize\[tmp\]\]++;  
                    tmp=tmp/seize\[tmp\];  
                }  
                tmp=k;  
                while(tmp!=1){  
                    numer\[seize\[tmp\]\]--;  
                    tmp=tmp/seize\[tmp\];  
                }  
            }  
        }  
        for(int i=0;i<m;i++){  
            int c,r;  
            scanf("%d%d",&c,&r);  
            if(c<r)continue;  
            if(c-r<r)r=c-r;  
            for(int j=c,k=1;k<=r;j--,k++){  
                int tmp=j;  
                while(tmp!=1){  
                    numer\[seize\[tmp\]\]--;  
                    tmp=tmp/seize\[tmp\];  
                }  
                tmp=k;  
                while(tmp!=1){  
                    numer\[seize\[tmp\]\]++;  
                    tmp=tmp/seize\[tmp\];  
                }  
            }  
        }  
        int fg=0;  
        for(int i=2;i<5005;i++){  
            if(numer\[i\]<0){  
                fg=1;  
                break;  
            }  
        }  
        if(fg){  
            puts("0");  
            continue;  
        }  
        fg=0;  
        bignum now,la(1),tmp;  
        for(int i=2;i<5001;i++){  
            if(!numer\[i\])continue;  
            now.clear();  
            tmp.clear();  
            now=now.add(1);  
            tmp=tmp.add(i);  
            for(int j=0;j<numer\[i\];j++){  
                now=now\*tmp;  
                if(now.flag){  
                    fg=1;  
                    break;  
                }  
            }  
            if(fg)break;  
            la=la\*now;  
            if(la.flag){  
                fg=1;  
                break;  
            }  
        }  
        if(fg)puts("-1");  
        else la.check();  
    }  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
}  

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。