LightOJ 1220 Mysterious Bacteria

来源:互联网 发布:防火墙 vlan 网络隔离 编辑:程序博客网 时间:2024/05/16 08:43
  1. 数论中常用质因数分解,要从数的本质变化出发思考问题。
  2. p即是e[]的最大公因数,因为每一项都可以除这个最大公因数。
  3. 负数的最大公因数不能是偶数,提前把2除尽。
  4. -2147483648用int取相反数还是-2147483648,因为会溢出,应该用longlong。

题目链接:http://acm.hust.edu.cn/vjudge/problem/26932

#pragma comment(linker,"/STACK:1024000000,1024000000")#include<cstdio>#include<iostream>#include<sstream>#include<cstdlib>#include<cmath>#include<cctype>#include<string>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<ctime>#include<vector>#include<fstream>#include<list>using namespace std;#define ms(s) memset(s,0,sizeof(s))typedef unsigned long long ULL;typedef long long LL;const int INF = 0x3fffffff;//the p's size = N/lnN;10^5 and 10^6 can use 10const int N = 1000000;bool primeTable[N+5];int p[N/10],tot;int e[N/10],tot2;int gcd(int a, int b){    return (b==0) ? a : gcd(b,a%b);}void make_primeTable(){    tot = 0;    fill(primeTable,primeTable+N,1);    primeTable[0] = false;    primeTable[1] = false;    int maxed = sqrt(N);    for(int i = 2; i <= maxed; ++i){        if(primeTable[i] == true){            p[tot++] = i;            for(int j = i*i; j <= N; j += i)                primeTable[j] = false;        }    }    for(int i = maxed+1; i <= N; ++i)        if(primeTable[i] == true)  p[tot++] = i;}void add_factor(LL n){    ms(e);    tot2 = 0;    if(n < 2)  return;    for(int i = 0; i < tot; ++i){        if(n%p[i] == 0)            tot2++;        while(n%p[i] == 0){            e[tot2]++;            n /= p[i];        }        if(n == 1)  break;    }    //if(n > 1){说明此时还有一个超出范围的素数,通常有且仅有一个,例如对10^12素因子分解枚举10^6次内的素数,那么>10^6的数只有一个}int main(){//    freopen("F:\\input.txt","r",stdin);//    freopen("F:\\output.txt","w",stdout);//    ios::sync_with_stdio(false);    make_primeTable();    int t;    LL n;    int ans;    scanf("%d",&t);    for(int cas = 1; cas <= t; ++cas){        scanf("%lld",&n);        if(n < 0){            n = -n;            add_factor(n);            for(int i = 1; i <= tot2; ++i){                while(e[i]%2 == 0)                    e[i]/=2;            }        }        else{            add_factor(n);        }        ans = e[1];        if(tot2 == 0)            ans = 1;        else{            for(int i = 2; i <= tot2; ++i){                ans = gcd(ans,e[i]);            }        }        printf("Case %d: %d\n",cas,ans);    }    return 0;}
0 0
原创粉丝点击