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. ubuntu采用apt-get安装pure-ftpd

    apt-get install pure-ftpdcd /etc/pure-ftpd/auth #开启虚拟账户登陆;ln -s 60auth ../conf/PureDB 60auth #查看虚拟账户 ...

  2. linux记录每次登陆的历史命令

    编辑/etc/profile,增加如下代码 #Record history operation USER_IP=`>/dev/null |awk '{print $NF}' |sed -e 's ...

  3. 怎样解决使用feof()函数时出现的问题?

    feof函数        昨天在做一个课程设计时,一个函数的功能是将文件中的数据一条条的读到链表中去.既然不确定有多少条数据,那只能借助feof()函数了,本来文件部分就没学好,也就知道这一个方法. ...

  4. 基本类型和引用类型的值 [重温JavaScript基础(一)]

    前言: JavaScript 的变量与其他语言的变量有很大区别.JavaScript 变量松散类型的本质,决定了它只是在特定时间用于保存特定值的一个名字而已.由于不存在定义某个变量必须要保存何种数据类 ...

  5. 爬虫(二)requests 登陆某检索网站

    1 import requests import os from PIL import Image import pytesseract import re rootUrl = xxx # 构建登录页 ...

  6. 20170813-CSRF 跨站请求伪造

    CSRF CSRF是Cross Site Request Forgery的缩写,翻译过来就是跨站请求伪造. 跨站:顾名思义,就是从一个网站到另一个网站. 请求:即HTTP请求. 伪造:在这里可以理解为 ...

  7. OpenCV图像增强(python)

    为了得到更加清晰的图像我们需要通过技术对图像进行处理,比如使用对比度增强的方法来处理图像,对比度增强就是对图像输出的灰度级放大到指定的程度,获得图像质量的提升.本文主要通过代码的方式,通过OpenCV ...

  8. 200行代码,7个对象——让你了解ASP.NET Core框架的本质[3.x版]

    2019年1月19日,微软技术(苏州)俱乐部成立,我受邀在成立大会上作了一个名为<ASP.NET Core框架揭秘>的分享.在此次分享中,我按照ASP.NET Core自身的运行原理和设计 ...

  9. Python知识点 - Xpath提取某个标签,需要转换为HTML。

        # lxml转Html from lxml import etree from HTMLParser import HTMLParser def lxml_to_html(text:etree ...

  10. 网络编程---socket模块

    内容中代码都是先写  server端, 再写 client端 1 TCP和UDP对比 TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效 ...