蓝桥杯小白赛第六期 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;
}
相关推荐
天天爱吃肉821812 分钟前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
alphaTao23 分钟前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode
甄心爱学习26 分钟前
【leetcode】判断平衡二叉树
python·算法·leetcode
颜酱36 分钟前
从二叉树到衍生结构:5种高频树结构原理+解析
javascript·后端·算法
不知名XL1 小时前
day50 单调栈
数据结构·算法·leetcode
@––––––1 小时前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划
xsyaaaan1 小时前
代码随想录Day31动态规划:1049最后一块石头的重量II_494目标和_474一和零
算法·动态规划
Jay Kay2 小时前
GVPO:Group Variance Policy Optimization
人工智能·算法·机器学习
Epiphany.5562 小时前
蓝桥杯备赛题目-----爆破
算法·职场和发展·蓝桥杯
YuTaoShao3 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展