HDU-1695 GCD exclusion Theorem

Source: Internet
Author: User

In this case, we must first divide the two sides of the equation by K, so that the number of mutual prime numbers on both sides is the final result. The strategy we adopt is to use each number between cells to match the number of large intervals. However, if a qualitative factor of a number is decomposed every time, it will be TLE. Therefore, the quality factor of each number from 1 to is pre-processed before calculation.

The Code is as follows:

#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int a, b, c, d, k, rec[300000], idx;int start[100005], end[100005], S, E;long long int ret;void cut(int x){    int LIM = (int)sqrt(double(x));    if (x % 2 == 0) {        rec[++idx] = 2;        while (x % 2 == 0) { x /= 2; }    }    for (int i = 3; i <= LIM; i += 2) {        if (x % i == 0) {            rec[++idx] = i;            while (x % i == 0) { x /= i; }        }    }    if (x != 1) { rec[++idx] = x; }}void pre(){    idx = 0;    for (int i = 1; i <= 100000; ++i) {        start[i] = idx;        cut(i);        end[i] = idx;    }}void dfs(int p, int num, int sign, int &sum, int l, int r){    if (p == E) {        if (num > 1) {            sum += sign * (r / num - (l-1) / num);        }        return;    }    dfs(p+1, num, sign, sum, l, r);    dfs(p+1, num*rec[p+1], -sign, sum, l, r);}int main(){    int T, ca = 0, sum, l, r;    pre();    scanf("%d", &T);    while (T--) {        ret = 0;        scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);        printf( "Case %d: ", ++ca);        if (!k) {            puts("0");            continue;        }        b /= k, d /= k;        if (b > d) {            int t = b;            b = d;            d = t;        }        for (int i = 1; i <= b; ++i) {            S = start[i], E = end[i];            sum = 0;            l = i, r = d;            dfs(S, 1, -1, sum, l, r);            ret += (r-l+1) - sum;        }        printf("%I64d\n", ret);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.