zoukankan      html  css  js  c++  java
  • HDU

    题意:

      给出a,b,c,d,k。a和c恒为1。求1~b,1~d中有多少数对的gcd值为k。(x,y)和(y,x)为同一数对(x≠y)。

    题解:

      这道题像HDU2841的变形。只要把b和d都除以k就基本差不多了。重叠部分除以2,注意(1,1)只有一个,要加上。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5+10;
    int t;
    int k;
    int u, v;
    int x, y;
    int len;
    int pnum;
    int p[maxn];
    bool e[maxn];
    ll res, ans;
    vector<int> g[maxn];
    void prime() {
        e[0] = e[1] = 1; pnum = 0;
        for(int i = 2; i < maxn; i++) {
            if(e[i]==0) p[++pnum] = i;
            for(int j = 1; j<=pnum && p[j]*i<maxn; j++) {
                e[p[j]*i] = 1;
                if(i%p[j]==0) break;
            }
        }
    }
    int gcd(int x, int y) {
        return y==0?x:gcd(y, x%y);
    }
    int lcm(int x, int y) {
        return x/gcd(x, y)*y;
    }
    void dfs(int cur, int tol, int sum, int id) {
        if(cur >= len) return ;
        int p = lcm(g[id][cur], sum);
        if(tol&1) res -= y/p;
        else res += y/p;
        dfs(cur+1, tol+1, p, id);
        dfs(cur+1, tol, sum, id);
    }
    int main() {
        prime();
        for(int i = 1; i <= pnum; i++) {
            int cnt = 1;
            while(cnt*p[i] <= 100000) {
                g[cnt*p[i]].push_back(p[i]);
                cnt++;
            }
        }
        scanf("%d", &t);
        for(int casee = 1; casee <= t; casee++) {
            scanf("%d%d%d%d%d", &u, &x, &v, &y, &k);
            if(!k || x<k || y<k) {
                printf("Case %d: 0
    ", casee);
                continue;
            }
            x /= k; y /= k;
            if(x < y) swap(x, y);
            ans = y;
            for(int i = 2; i <= x; i++) {
                res = 0;
                len = g[i].size();
                dfs(0, 0, 1, i);
                ans += y-res;
                if(i==y) ans = ans/2+1;
                
            }
            printf("Case %d: %lld
    ", casee, ans);
        }
    }
    View Code
  • 相关阅读:
    LeetCode 252. Meeting Rooms
    LeetCode 161. One Edit Distance
    LeetCode 156. Binary Tree Upside Down
    LeetCode 173. Binary Search Tree Iterator
    LeetCode 285. Inorder Successor in BST
    LeetCode 305. Number of Islands II
    LeetCode 272. Closest Binary Search Tree Value II
    LeetCode 270. Closest Binary Search Tree Value
    LeetCode 329. Longest Increasing Path in a Matrix
    LintCode Subtree
  • 原文地址:https://www.cnblogs.com/Pneuis/p/8837065.html
Copyright © 2011-2022 走看看