codeforces 992 B (有关GCD、LCM,变量写成倍数形式,除法缩小数据范围)

题目:http://codeforces.com/contest/992/problem/B

题意:输入l,r,x,y,要求在区间 [l,r] 中寻找两个数 a,b 使得GCD(a,b)==x,LCM(a,b)==y,问这样的(a,b)有多少对(其中当a!=b时(a,b)和(b,a)是不同的)。数据范围(1 \leq l \leq r \leq 10^{9}, 1 \leq x \leq y \leq 10^{9})

思路:设 a=nx,b=my  (其中m,n必须满足 __gcd(n,m)==1,因为 \tfrac{ab}{x}=y,即 \tfrac{nmx^{2}}{x}=y ,即 nm=\tfrac{y}{x} 

所以 if(y%x) {cout<<0<<endl;return 0;} (n*m是整数,找不到这样的n,m即没有符合条件的a,b)

因为显然  \frac{y}{x}\leq 10^{9},对n,m暴力遍历一下即可,代入a=n*x,b=m*y,

1. 限制条件 : __gcd(n,m)==1 && a<b && l<=a && b<=r  这样最多也就跑 \sqrt{10^{9}} 次   

2. ans=ans*2 (即 a>b 时 ,a,b的值交换位置)    另外判断 当 n==m 时 即 a==b时 是否符合 "1"中的限制条件 若也符合 就 ans++ 

 

(ps: 最初 一直在 a*b = x*y 这个角度想,想有没有不等式或其他条件可以缩小 x*y = 10^{18} 这么大的数据范围 当初的错误点有:

1.忽略掉了 a*b==x*y 的前提是 GCD(a,b)==x ,在写的时候没有写这个条件

2.相乘数据范围 既然 反而变大了(感觉这么做和 直接a从l遍历到r没有区别),就应该想一下能否化为除法 缩小数据范围

 )

#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mkp(a,b) make_pair(a,b)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define fi first
#define se second
#define lc (d<<1) //d*2
#define rc (d<<1|1) //d*2+1
#define eps 1e-9
#define dbg(x) cerr << #x << " = " << x << "\n";
#define mst(a,val) memset(a,val,sizeof(a))
#define stn(a) setprecision(a)//小数总有效位数
#define stfl setiosflags(ios::fixed)//点后位数:cout<<stfl<<stn(a);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI=3.1415926535897932;
const int MAXN=1e5+10;
const ll mod=1e9+7;
ll inline mpow(ll a,ll b){ll ans=1;a%=mod;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod,b>>=1;}return ans;}
int inline sgn(double x){return (x>-eps)-(x<eps);} //a<b:sgn(a-b)<0
priority_queue<int,vector<int>,greater<int> > qu; //up
priority_queue<int,vector<int>,less<int> > qd; //dn
const int inf = 0x3f3f3f3f; //9
const ll inff = 0x3f3f3f3f3f3f3f3f; //18

ll l,r,x,y;
ll a,b;
ll ans=0;

int main()
{
    fio;
    cin>>l>>r>>x>>y;
    ll sum=x*y;
    if(y%x) {cout<<0<<endl;return 0;}
    ll k=y/x;
    ll n=1,m=k;
    int flag=0;
    while(n<=m)
    {
        ll a=n*x,b=m*x;
        if(n*m==k&&l<=a&&a<b&&b<=r&&__gcd(n,m)==1)
            ans++;
        if(n*m==k&&l<=a&&a==b&&b<=r&&__gcd(n,m)==1)
            ans++,flag=1;
        n++;
        m=k/n;
    }
    ans*=2;
    if(flag) ans--;
    cout<<ans<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值