当前位置: 首页 > news >正文

cf2067A

 原题链接:https://codeforces.com/contest/2067/problem/A
题目背景:

        给定x,y,判读是否存在 n 满足S(n) = x,S(n + 1) = y。定义 S(a) 等于 a 的十进制位数之和。

思路:

        不难发现一般 n 和 n + 1 的位数之和相差为 1,这种情况我们先判断一下即可;但是当 n 以 9结尾时,他在+1时就会进位,所有我们可以推出 S(n) = S(n+1) + k * 9 - 1( k 为 n 中有几个 9 )通过公式变形可得(S(n) - S(n +1) +1)/9 = k,带入 x,y 得( x - y + 1 ) / 9 = k,所以只需判断 (x - y + 1)% 9 是否等于 0 即可。

数据范围:

        1 <= x,y <= 1000

时间复杂度:

        O(1)。

ac代码: 
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* 有乘就强转,前缀和开ll */void solve()
{int x, y;cin >> x >> y;if (x == y)cout << "NO" << endl;else if (x + 1 == y)cout << "YES" << endl;else if (x > y && (x - y + 1) % 9 == 0)cout << "YES" << endl;elsecout << "NO" << endl;
}int main()
{ioscc;int T;cin >> T;while (T--)solve();return 0;
}

http://www.xdnf.cn/news/721567.html

相关文章:

  • 定位例子(vue3)
  • 告别RAG上下文丢失:Late Chunking 与 Contextual Retrieval 深度对比解析
  • 【实证分析】上市公司全要素生产率+5种测算方式(1999-2024年)
  • OTA中版本灰度发布、用户反馈闭环浅谈
  • 深度解构:Profinet转Profibus网关如何重塑产品分离装置的控制逻辑
  • 【测试】设计测试⽤例方法
  • 键盘录入的两套体系区别(Random)
  • 【速通RAG实战:进阶】16、AI生成思维导图全技术解析
  • SpringBoot(五)--- 异常处理、JWT令牌、拦截技术
  • python的高级2——函数作为对象
  • ⚽【足球数据全维度解析】从基础统计到高阶分析,数据如何重塑现代足球?
  • 中国国运新引擎:下一代液晶技术突破如何重塑全球显示格局
  • 通过粘性布局实现表格且带有固定列
  • 文件夹的命名与分类
  • Geoserver修行记--点击geoserver服务的WMTS能力(GetCapabilities)文档显示400 null
  • 第五十九节:性能优化-GPU加速 (CUDA 模块)
  • 2025-5-27Vue3快速上手
  • 软考-系统架构设计师-第八章 数据库设计基础知识
  • Lesson 25 Do the English speak English
  • DMBOK对比知识点对比(1)
  • 中国头盔护具展在杭州举办合适
  • 操作系统 Windows Linux macOS如何查看Ollama的存储位置
  • IP地址交换如何让车联网效率翻倍?
  • Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
  • 利用Python直接生成html注意事项
  • 从“无差别降噪”到“精准语音保留”:非因果优化技术为助听设备和耳机降噪注入新活力
  • SAR ADC 比较器噪声分析(一)
  • sensevoice sherpa-onnx部署
  • 嵌入式学习笔记 - freeRTOS任务优先级抢占,时间片抢占的实现机制
  • shell脚本总结12:自定义函数