蓝桥杯小白赛第六期 4.你不干?有的是帕鲁干!

4.你不干?有的是帕鲁干!【算法赛】 - 蓝桥云课 (lanqiao.cn)

这是我的代码,求出样例了,但是过不了:

js 复制代码
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
typedef long long LL;
int main()
{
	int n; cin >> n;
	string s;
	for (int i = 0; i < n; i++)
	{
		cin >> s;

		for (int i = 1; i < 100000000000000000; i ++)
		{

			LL temp =  pow(i + 2, 2) - pow(i, 2);
			if (s == to_string(temp))
			{
				cout << "Yes\n"; cout << i << " " << i + 2 << endl;
				break;
			}
			else
			{
				cout << "No\n";
				break;
			}
		}
	}

	return 0;
}

思想

一。 <math xmlns="http://www.w3.org/1998/Math/MathML"> b − a = 2 b-a=2 </math>b−a=2,因为两个连续奇数差为2

二。 <math xmlns="http://www.w3.org/1998/Math/MathML"> b = a + 2 b=a+2 </math>b=a+2

我们现在来求 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) (b+a) </math>(b+a)

<math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) = ( a + 2 + a ) = ( 2 a + 2 ) = 2 ( a + 1 ) (b+a)=(a+2+a)=(2a+2)=2(a+1) </math>(b+a)=(a+2+a)=(2a+2)=2(a+1)

因为a+1肯定是偶数(因为a是奇数),所以2(a+1)一定至少是4。

因此 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) (b+a) </math>(b+a)一定可以被4整除。 又因为 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b − a ) (b-a) </math>(b−a)是2所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) ( b − 2 (b+a)(b-2 </math>(b+a)(b−2)至少是8

所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> b 2 − a 2 b^2-a^2 </math>b2−a2一定可以至少被8整除所以我们要求的数一定可以被8整除

code

js 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
  int t;cin>>t;
  while(t--){
      int x;cin>>x;
      if(x%8==0&&x!=0){
          cout<<"Yes"<<"\n";
          cout<<2*(x/8)-1<<" "<<2*(x/8)+1<<"\n";
      }else{
          cout<<"No"<<"\n";
      }
  }
  return 0;
}
相关推荐
xin007hoyo3 小时前
算法笔记·数学·最大公约数
笔记·算法
丶Darling.3 小时前
Day126 | 灵神 | 二叉树 | 层数最深的叶子结点的和
数据结构·c++·算法·二叉树·深度优先
小O的算法实验室3 小时前
2025年ESWA SCI1区TOP,离散人工蜂群算法+多农场多除草机器人任务分配,深度解析+性能实测
算法·智能算法改进
2401_859049084 小时前
Git使用
arm开发·git·stm32·单片机·mcu·算法
冠位观测者5 小时前
【Leetcode 每日一题】2942. 查找包含给定字符的单词
算法·leetcode·职场和发展
蓝心湄5 小时前
C语言-枚举
c语言·开发语言·算法
轮到我狗叫了5 小时前
力扣小题, 力扣113.路径总和II力扣.111二叉树的最小深度 力扣.221最大正方形力扣5.最长回文子串更加优秀的算法:中心扩展算法
算法·leetcode·深度优先
haven-8526 小时前
最大似然估计(Maximum Likelihood Estimation, MLE)详解
算法·机器学习·概率论
逐光沧海6 小时前
排序和排列——蓝桥杯备考
算法·蓝桥杯
小琪琪:)6 小时前
【刷题】质数因子
算法