zoukankan      html  css  js  c++  java
  • GCD (区间数的质因子打表+容斥原理)

    GCD

     HDU - 1695 

    Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs. 
    Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same. 

    Yoiu can assume that a = c = 1 in all test cases. 

    InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases. 
    Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above. 
    OutputFor each test case, print the number of choices. Use the format in the example. 
    Sample Input

    2
    1 3 1 5 1
    1 11014 1 14409 9

    Sample Output

    Case 1: 9
    Case 2: 736427
    

     题意:求[a,b]区间和[c,d]区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d)。(本题中a,b均为1)

    题解:

      令区间端点均除以k,那么就变成了求[a/k,b/k]区间里面和[c/k,d/k]区间里面gcd(x,y)==1的对数,即求两个区间里面互质的数的对数,因为1 2和2 1算是一对,所以我们需要把相对小的区间遍历一遍,大的区间从小区间+1开始遍历,即可避免这个问题。

      注意先对每个数的质因子打个表,不然会超时。

      注意排除k==0的情况。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<vector>
      5 #include<algorithm>
      6 using namespace std;
      7 typedef long long ll;
      8 const ll maxn=1e5+10;
      9 ll n,cnt;
     10 ll ans;
     11 vector<ll>prime[maxn];
     12 ll l1,l2,r1,r2,k;
     13 ll l,r;
     14 bool vis[maxn];
     15 ll gcd(ll a,ll b)
     16 {
     17     if(b==0)
     18         return a;
     19     else
     20         return gcd(b,a%b);
     21 }
     22 ll Lcm(ll a,ll b)
     23 {
     24     return a*b/gcd(a,b);
     25 }
     26 ll get_prime(ll x)
     27 {
     28     ll t=x;
     29     for(ll i=2;i*i<=x;i++)
     30     {
     31         if(x%i)
     32             continue;
     33         while(x%i==0)
     34             x/=i;
     35         prime[t].push_back(i);
     36     }
     37     if(x!=1)
     38         prime[t].push_back(x);
     39 }
     40 ll casen;
     41 void dfs(ll m,ll x,ll th,ll now,ll step)
     42 {
     43 
     44     ll lcm=Lcm(prime[x][th],now);
     45     if(step&1)
     46     {
     47         ans+=m/lcm;
     48     }
     49     else
     50     {
     51         ans-=m/lcm;
     52     }
     53     for(ll i=th+1;i<prime[x].size();i++)
     54         dfs(m,x,i,lcm,step+1);
     55 }
     56 int main()
     57 {
     58     ll casen;
     59     scanf("%lld",&casen);
     60     ll ca=1;
     61     for(ll i=2;i<maxn;i++)//注意先打表 
     62     {
     63         get_prime(i);
     64     }
     65     while(casen--)
     66     {
     67         ans=0;
     68         cnt=0;
     69         scanf("%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&k);
     70         if(k==0)//注意判零 
     71         {
     72             printf("Case %lld: 0
    ",ca++);
     73             continue;
     74         }
     75         r1/=k;
     76         r2/=k;
     77         if(r1>r2)
     78             swap(r1,r2);//注意判断两个区间谁大谁小 
     79         ll sum=0;
     80         for(ll i=l1;i<=r1;i++)
     81         {
     82     
     83             ans=0;
     84             for(ll j=0;j<prime[i].size();j++)
     85             {
     86                 dfs(i-1,i,j,prime[i][j],1);
     87             }
     88             ll le=i-1-ans;
     89             ans=0;
     90             for(ll j=0;j<prime[i].size();j++)
     91             {
     92                 dfs(r2,i,j,prime[i][j],1);
     93             }
     94             ll ri=r2-ans;
     95             sum+=ri-le;
     96         }
     97 
     98         printf("Case %lld: %lld
    ",ca++,sum);
     99     }
    100 }
  • 相关阅读:
    计时器
    练习
    实现跨层组件通信(Vue3___defineComponent)
    微信内置浏览器h5监听手机返回键
    ECharts柱状图线形图
    easyui日期到月份
    uni-app传参
    js浏览器唯一标识
    知网论文查重如何查呢,免费的是真的吗?
    淘宝检测论文查重可靠吗?怎么检验?
  • 原文地址:https://www.cnblogs.com/1013star/p/9896262.html
Copyright © 2011-2022 走看看