蓝桥杯小白赛第六期 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;
}
相关推荐
昵称小白4 小时前
复杂度分析方法
算法
科研前沿4 小时前
2026 数字孪生前沿科技:全景迭代报告 —— 镜像视界生成式孪生(Generative DT)技术白皮书
大数据·人工智能·科技·算法·音视频·空间计算
学涯乐码堂主6 小时前
有趣的“打擂台算法”
c++·算法·青少年编程·gesp
Tutankaaa6 小时前
知识竞赛题库设计全攻略
人工智能·算法
WolfGang0073217 小时前
代码随想录算法训练营 Day50 | 图论 part08
数据结构·算法·图论
aini_lovee9 小时前
多目标粒子群优化(MOPSO)双适应度函数MATLAB实现
人工智能·算法·matlab
yong99909 小时前
图像融合与拼接:完整MATLAB工具箱
算法·计算机视觉·matlab
春风不语5059 小时前
深入理解主成分分析(PCA)
算法
apollowing9 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(二十二)
算法·启发式算法·web app
晚枫歌F9 小时前
最小堆定时器
数据结构·算法