CSDN同步

原题链接

简要题意:

多组数据,问能否把 \(n\) 分为 \(k\) 个 不同的 正奇数之和。

盲猜数学结论题。

只要考虑两个问题:

  1. \(n\) 的大小是否足够。

  2. \(n\) 的奇偶性是否满足。

对于第 \(1\) 条,最小的 \(k\) 个 不同的 正奇数之和为 \(k^2\).(这都不知道,建议重学小学数学)

所以,\(n \geq k^2\) 才可能有解。

对于第 \(2\) 条,因为 \(k\) 个正奇数与 \(k\) 的奇偶性相同,所以必须满足:

\[n \equiv k \pmod 2
\]

这两条同时满足即有解,否则无解。

为什么呢?

考虑一种拆分:

\[1 + 3 + 5 + ... + (2k-3) + p = n
\]

即前 \(k-1\) 个正奇数加上另一个奇数。

如果 \(n\) 同时满足上两条,则显然存在这样的拆分,且 \(p \geq 2 \times k - 1\),也不存在重复。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std; typedef long long ll; inline ll read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
ll x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;} int main(){
ll T=read(),n,k; while(T--) {
n=read(),k=read();
if(n<k*k || (n-k)&1) puts("NO");
else puts("YES");
} //别忘了,k*k会爆int,开上 long long
return 0;
}

CF1327A Sum of Odd Integers 题解的更多相关文章

  1. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  2. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  3. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  4. LeetCode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. leetcode371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  6. 【LeetCode】Sum of Two Integers

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  7. 每天一道LeetCode--371. Sum of Two Integers

    alculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Examp ...

  8. 371. Sum of Two Integers -- Avota

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  9. Leetcode 371: Sum of Two Integers(使用位运算实现)

    题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

随机推荐

  1. Python 代码实现验证码识别

    Python 代码实现验证码识别 测试开发社区  1周前 源 /  j_hao104 一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… ...

  2. json_encode在设计api时需要注意的问题

    1. 在设计api时我们经常会使用关联数组,例如:我要返回给客户端主题信息和主题包列表 原始数组格式 $arr = array( 100=>array('themeName'=>'a',' ...

  3. 模拟HTTP请求超时时间设置

    HTTP请求有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间(请求资源超时时间). 使用curl命令行 连接超时时间用 --connect-timeout 参数来指定 数据传输的最大 ...

  4. webpack的基本配置(初识)

    webpack能根据模块的依赖关系递归地构建一个依赖关系图,当中包含了应用程序所需要的所有模块,最后打包成一个或多个bundle.它有四个核心概念entry.output .loader.plugin ...

  5. C语言链表的基本操作

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表

    上一节我们介绍了如何在Spring Boot中使用模板引擎Thymeleaf开发Web应用的基础.接下来,我们介绍一下后端开发经常会遇到的一个场景:可视化图表. 通常,这类需求在客户端应用中不太会用到 ...

  7. 30分钟学会Objective-C

    注: 本文首发于我的个人博客:https://evilpan.com/2019/04/05/objc-basics/ 请原谅我的标题党.但是如果你有其他语言的学习经验,要学习Objective-C的语 ...

  8. vue——一个页面实现音乐播放器

    请忽略下面这段文字年关将至,时间好歹又多出了些许.却不敢过度消遣.岁月未曾饶过我,我亦不想饶过岁月.且将它塞得膨胀,让这一年看似加更充实.不曾料想我一个爱些风花雪月.研墨行歌之人,却做起了碼农这一行当 ...

  9. HTML常用标签的使用

    一.常见标签详解 1.<iframe>标签 HTML内联框架元素 <iframe> 表示嵌套的浏览上下文,有效地将另一个HTML页面嵌入到当前页面中.在HTML 4.01中,文 ...

  10. 一步步去阅读koa源码,整体架构分析

    阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想.大到架构设计,小到可取的命名风格,还有设计模式.实现某类功能使用到的数据结构和算法等等. 使用koa 其实某个框架阅读源码的时候,首 ...