Couple number
题目描述
对于一个整数 n n n(可负可正),若存在两个自然数 x , y x, y x,y,满足 n = x 2 − y 2 n = x^2 - y^2 n=x2−y2,则称 n n n 是 Couple number。
给出 a , b a,b a,b,请求出 [ a , b ] [a, b] [a,b] 范围内有多少个 Couple number。
输入格式
输入只有一行两个整数,分别表示 a , b a, b a,b。
输出格式
输出一行一个整数表示答案。
样例 #1
样例输入 #1
1 10
样例输出 #1
7
提示说明
数据规模与约定
对于全部的测试点,保证 − 1 0 7 ≤ a < b ≤ 1 0 7 -10^7 \le a < b \le 10^7 −107≤a<b≤107 , b − a ≤ 1 0 7 b - a \le 10^7 b−a≤107。
题目解析
推式子,得到 $x = ( y − z ) × ( y + z ) $,可知 x x x 满足可以拆成 2 2 2个奇偶性相同的数的乘积。
- 如果是奇数,直接拆成 1 1 1 和它本身即可。
- 如果是偶数,因为要拆成 2 2 2个偶数,所以应是 4 4 4 的倍数,此时一种拆分为拆成 2 2 2和 x x x除以 2 2 2。
- 至此,答案为 L LL到R RR 中奇数和 4 4 4 的倍数的个数。
代码内容
//#include <iostream>
//#include <algorithm>
//#include <string>
//#include <cmath>
//#include <ctime>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a,b;
cin>>a>>b;
ll index=0;
for(ll i=a;i<=b;i++)
{
if(i%2!=0)
index++;
else if(i%4==0)
index++;
}
cout<<index<<endl;
return 0;
}