蓝桥杯小白赛第六期 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;
}
相关推荐
Sunsets_Red2 分钟前
差分操作正确性证明
java·c语言·c++·python·算法·c#
【杨(_> <_)】40 分钟前
SAR信号处理重要工具-傅里叶变换(二)
算法·信号处理·傅里叶分析·菲涅尔函数
怎么没有名字注册了啊1 小时前
爬动的蠕虫
算法
取酒鱼食--【余九】1 小时前
机器人学基础(一)【坐标系和位姿变换】
笔记·算法·机器人·开源·机器人运动学·机器人学基础
晨非辰1 小时前
【面试高频数据结构(四)】--《从单链到双链的进阶,读懂“双向奔赴”的算法之美与效率权衡》
java·数据结构·c++·人工智能·算法·机器学习·面试
im_AMBER1 小时前
数据结构 03 栈和队列
数据结构·学习·算法
凸头1 小时前
以AtomicInteger为例的Atomic 类的底层CAS细节理解
java·jvm·算法
前端小刘哥2 小时前
赋能在线教育与企业培训:视频直播点播平台EasyDSS视频点播的核心技术与应用实践
算法
吗~喽2 小时前
【LeetCode】四数之和
算法·leetcode·职场和发展
Net_Walke2 小时前
【散列函数】哈希函数简介
算法·哈希算法