zoukankan      html  css  js  c++  java
  • CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)

    http://codeforces.com/problemset/problem/992/B

     

     题意:

    给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lcm(a,b)=y ,如果a,b交换位置就是不同的数对 

    思路:

    根据lcm(最小公倍数) 的定义 y=a*b/x; 也就是说 x∗y=a∗b ;

    那么 ,我们发现a,b一定为y的因数,所以我们枚举y的每个因子就可以,我们只要用log(y)的复杂度暴力算每一个因数就可以 ,

    然后对于每个因子当做a, b=x*y/a; 然后判断a,b是否在区间内,gcd(a,b)是否为x,(注意要判断是否等于b)

     

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 //const double PI=acos(-1);
    17 #define Bug cout<<"---------------------"<<endl
    18 const int maxm=1e6+10;
    19 const int maxn=1e5+10;
    20 using namespace std;
    21 
    22 LL gcd(LL a,LL b)
    23 {
    24     return b? gcd(b,a%b):a;
    25 }
    26 
    27 int main()
    28 {
    29     int l,r,x,y;
    30     scanf("%d %d %d %d",&l,&r,&x,&y);
    31     int ans=0;
    32     for(LL i=1;i*i<=y;i++)//第一个因子 
    33     {
    34         if(y%i==0)
    35         {
    36             LL j=x*(y/i);
    37             if(i>=l&&i<=r&&j>=l&&j<=r&&gcd(i,j)==x)
    38                 ans++;
    39             LL ii=y/i;//对应的另一个因子 
    40             if(i!=ii)
    41             {
    42                 LL jj=x*(y/ii);
    43                 if(ii>=l&&ii<=r&&jj>=l&&jj<=r&&gcd(ii,jj)==x)
    44                     ans++;
    45             }    
    46         }
    47     }         
    48     printf("%d
    ",ans);
    49     return 0;
    50 }

    Hankson的趣味题

    Description

      Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson。现 在,刚刚放学回家的Hankson 正在思考一个有趣的问题。 今天在课堂上,老师讲解了如何求两个正整数c1 和c2 的最大公约数和最小公倍数。现 在Hankson 认为自己已经熟练地掌握了这些知识,他开始思考一个“求公约数”和“求公 倍数”之类问题的“逆问题”,这个问题是这样的:已知正整数a0,a1,b0,b1,设某未知正整 数x 满足: 1. x 和a0 的最大公约数是a1; 2. x 和b0 的最小公倍数是b1。 Hankson 的“逆问题”就是求出满足条件的正整数x。但稍加思索之后,他发现这样的 x 并不唯一,甚至可能不存在。因此他转而开始考虑如何求解满足条件的x 的个数。请你帮 助他编程求解这个问题。

    Input

      输入第一行为一个正整数n,表示有n 组输入数据。
    接下来的n 行每 行一组输入数据,为四个正整数a0,a1,b0,b1,每两个整数之间用一个空格隔开。输入 数据保证a0 能被a1 整除,b1 能被b0 整除。

    Output

      输出共n 行。每组输入数据的输出结果占一行,为一个整数。
    对于每组数据:若不存在这样的 x,请输出0; 若存在这样的 x,请输出满足条件的x 的个数;

    Sample Input

    2
    41 1 96 288
    95 1 37 1776

    Sample Output

    6
    2

    HINT

    样例说明

    第一组输入数据,x 可以是9、18、36、72、144、288,共有6 个。

    第二组输入数据,x 可以是48、1776,共有2 个。



    数据规模和约定

    对于 50%的数据,保证有1≤a0,a1,b0,b1≤10000 且n≤100。

    对于 100%的数据,保证有1≤a0,a1,b0,b1≤2,000,000,000 且n≤2000。

    题解:

    https://www.cnblogs.com/five20/p/8434085.html

    代码如下(无优化):

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 //const double PI=acos(-1);
    17 #define Bug cout<<"---------------------"<<endl
    18 const int maxn=1e5+10;
    19 using namespace std;
    20 
    21 LL gcd(LL a,LL b)
    22 {
    23     if(b==0) return a;
    24     else return gcd(b,a%b);
    25 }
    26 
    27 int main()
    28 {
    29     int T;
    30     scanf("%d",&T);
    31     while(T--)
    32     {
    33         LL a,b,c,d;
    34         scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    35         if(a%b||d%c||d%b)
    36             printf("0
    ");
    37         else
    38         {
    39             int num=0;
    40             for(int x=1;x*x<=d;x++)
    41             {
    42                 if(d%x==0)
    43                 {
    44                     if(x%b==0&&gcd(x/b,a/b)==1&&gcd(d/x,d/c)==1) num++;
    45                     int y=d/x;
    46                     if(x==y) continue;
    47                     if(y%b==0&&gcd(y/b,a/b)==1&&gcd(d/y,d/c)==1) num++;
    48                 }
    49             }
    50             printf("%d
    ",num);    
    51         }    
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    闭包详解
    年少不知富婆好,错把少女当成宝
    var a = ? if(a==1 && a==2 && a==3){ console.log(1); }
    vue中watch监听的handler,deep,immediate用法详解
    前端VSCode常用插件-快捷键-以及常用技巧
    如何看待 Web 开发构建工具 Vite?-------------转载自知乎
    如何关联多个远程仓库
    图解 | 原来这就是网络
    QMdiArea、QMdiSubWindow
    QDockWidget
  • 原文地址:https://www.cnblogs.com/jiamian/p/11747813.html
Copyright © 2011-2022 走看看