累死自己卷死室友


私信TA

用户名:52hertz

访问量:11390

签 名:

刷题好!

等  级
排  名 1694
经  验 2661
参赛次数 2
文章发表 3
年  龄 0
在职情况 学生
学  校 吉林大学
专  业 软件工程

  自我简介:

解题思路:

        规律:只有当x为奇数或4的倍数时才能拆分为两个数的平方差。

注意事项:

        刚开始用c++写循环的时候,有一个样例会超时,故进一步寻找规律:F(X)=x/4+(x+1)/2,该式代表不大于x的满足条件的数的个数,用F(R)-F(L-1)即为L-R之间(大于等于L,小于等于R)满足条件的数的个数。


参考代码:

#include<iostream>
using namespace std;
int F(int x) {
	return x / 4 + (x + 1) / 2;//不大于x的满足条件的数的个数
}
int main() {
	int l = 0, r = 0;
	cin >> l >> r;
	cout << F(r)-F(l-1);
	return 0;
}


 

0.0分

98 人评分

  评论区

大佬,太六了
2024-01-12 18:40:34
#include<bits/stdc++.h>
using namespace std;
void work(){
	int l,r;cin>>l>>r;
	int sum=r-l+1;
	while(l%4!=2) l++;
	while(r%4!=2) r--;
	int d=(r-l)/4+1;
	cout<<sum-d<<"\n";
}
signed main(){
	work();
}
2023-12-10 14:00:44
6
2023-11-28 14:47:24
这个为什么不行?
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	int L, R, count = 0;
	int a[2];
	int i = 0;
	char c;
	cin >> a[0];
	while ((c=getchar()) != '\n')
	{
			cin >> a[i+1];
			i++;
	}
	L = a[0]; R = a[1];
	for (int num = L; num <= R; num++)
	{
		int q = ceil(sqrt(num));
		for (int j = q; j >= 0; j--)
		{
			int z = q*q - j*j;
			if (z == num)
			{
				count++;
				continue;
			}
		}
	}
	cout << count<<endl;
	system("pause");
	return 0;
}
2023-11-12 11:19:25
x/4表示不大于x的正整数里4的倍数的个数,(x+1)/2表示奇数的个数,好厉害
2023-11-08 21:01:49
6
2023-11-07 16:21:21
6
2023-10-05 14:47:07