列表

详情


NC237534. GCD

描述

Bob is interested in GCDs (Greatest Common Divisors). Formally, for some positive integers (a_1,...,a_k), we denote as the greatest positive integer d, such that for each .

Bob has chosen an interval . He is going to choose k distinct integers from the interval and compute their GCD. There are many integers that can be the final answer, and Bob is curious in the number of such integers. Please write a program to tell him the answer.

输入描述

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 1,2,3 as the final GCD, by choosing numbers (7,9), (6,8) and (6,9). It turns out that any other integer cannot become the GCD, thus the answer is 3.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

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;
}

上一题