NC237534. GCD
描述
输入描述
The first and only line of input consists of three integers .
输出描述
Print a single integer, indicating your answer.
示例1
输入:
5 9 2
输出:
3
说明:
For the sample, it is possible to get as the final GCD, by choosing numbers and . It turns out that any other integer cannot become the GCD, thus the answer is .C++(g++ 7.5.0) 解法, 执行用时: 89ms, 内存消耗: 408K, 提交时间: 2022-10-13 13:20:16
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll l, r, k; int main() { cin>>l>>r>>k; ll res=0; for(ll L=1, R, dl, dr; L<=r; L=R+1) { dl=(l-1)/L; dr=r/L; R=r/dr; if(dl>0) R=min(R, (l-1)/dl); if(dr-dl>=k) res+=(R-L+1); } cout<<res; return 0; }
C(gcc 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 328K, 提交时间: 2022-10-01 09:39:59
#include<stdio.h> int main() { long long l,r,k,z,y; scanf("%lld%lld%lld",&l,&r,&k); l--; long long d=r-l,ans; ans=d/k; z=ans; long long t=l/ans; while(t>=0&&z<=d){ y=r/(t+k); if(y>z)ans=ans+y-z; if(t!=0)z=l/t; t--; } printf("%lld\n",ans); return 0; }
C++(clang++ 11.0.1) 解法, 执行用时: 101ms, 内存消耗: 496K, 提交时间: 2022-10-11 15:06:02
#include<bits/stdc++.h> #define int long long using namespace std; signed main() { int l,r,k; cin>>l>>r>>k; int s=0; l--; for(int x=1,y;x<=r;x=y+1) { if(x<=l) y=min(r/(r/x),(l/(l/x))); else y=r/(r/x); if(r/x-l/x>=k) s+=y-x+1; } cout<<s<<endl; }