#include #include struct num_info { int a; int pr_n; int *pr; int *pow; int lcm; }; int *Pr = 0; int *Pow = 0; int Pr_end = 0; int Factor_max = 14; // 2^13 = 8192 // 2^14 = 16384 void factor_init(int N) { Pr = (int*)malloc(sizeof(int)*N*Factor_max*2); Pow = &(Pr[N*Factor_max]); return; } void factor(struct num_info *A) { A->pr = &(Pr[Pr_end]); A->pow = &(Pow[Pr_end]); A->pr_n = 0; if (A->a >= 2) { int N = A->a; int pow; for (pow=0; N%2 == 0; pow++) { N/=2; } if (pow != 0) { A->pr[A->pr_n] = 2; A->pow[A->pr_n] = pow; A->pr_n++; } int i; for (i=3; i*i<=N; i+=2) { for (pow=0; N%i == 0; pow++) { N/=i; } if (pow != 0) { A->pr[A->pr_n] = i; A->pow[A->pr_n] = pow; A->pr_n++; } } if (N != 1) { A->pr[A->pr_n] = N; A->pow[A->pr_n] = 1; A->pr_n++; } } else { A->pr[A->pr_n] = 1; A->pow[A->pr_n] = 1; A->pr_n++; } Pr_end += A->pr_n; return; } int *Lcm_pr = 0; int *Lcm_pow = 0; void lcm_init(struct num_info *A, int N) { int i; Lcm_pr = (int*)malloc(sizeof(int)*Factor_max*2*2); Lcm_pow = &(Lcm_pr[Factor_max*2]); return; } void lcm(struct num_info *A, struct num_info *B) { if (A->a == 1) { B->lcm = B->a; } else if (B->a == 1) { B->lcm = A->a; } else { int i = 0; int j = 0; int k = 0; while (1) { if (A->pr[j] < B->pr[k]) { j++; } else if (A->pr[j] > B->pr[k]) { k++; } else { if (A->pow[j] >= B->pow[k]) { Lcm_pr[i] = B->pr[k]; Lcm_pow[i] = B->pow[k]; i++; j++; k++; } else { Lcm_pr[i] = A->pr[j]; Lcm_pow[i] = A->pow[j]; i++; j++; k++; } } if (j >= A->pr_n) { break; } if (k >= B->pr_n) { break; } } int end = i; int gcd = 1; for (i=0; ilcm = A->a * B->a / gcd; } return; } int search_min(struct num_info *A, int N) { int i; int min = ~(1 << 31); int min_i; for (i=0; i= 3) { lcm_init(A, N); int j, k; for (i=0; i 0) { struct num_info W; W = A[i+1]; A[i+1] = A[(i+1)+k]; A[(i+1)+k] = W; } } } for (i=0; i