当前位置:网站首页>Codeforces Round #802 B. Palindromic Numbers
Codeforces Round #802 B. Palindromic Numbers
2022-07-18 09:24:00 【追烽】
https://codeforces.com/contest/1700/problem/B
標簽
構造, 高精度
題意
給定一個長度為 n 的長整數, 求出一個長度為 n (沒有前導零), 且和它相加為回文串的大整數.
思路
注意所求數的長度必須與給定數相同.
若最高比特不是 9, 可以讓回文串的所有比特置為 9.
若最高比特是 9, 不得不進比特, 可以讓回文串的所有比特置為 1.
然後用回文串减去給定數獲得答案.
代碼

#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
struct INT{
vector<int> X;
int r;//r餘數
bool neg;//負數
bool less(INT B){
//X<B.X, 兩者都為正數
if (X.size() != B.X.size()) return X.size() < B.X.size();
for (int i = X.size() - 1; i >= 0; i -- ) {
if(X[i] != B.X[i]) return X[i] < B.X[i];
}
return false;
}
void sub(INT B){
// X -= B, 支持小减大, 兩者都為正數
vector<int> C;
int Cneg;
if(less(B)) {
Cneg=true; swap(X,B.X);}
else Cneg=false;
for (int i = 0, t = 0; i < X.size(); i ++ ){
t = X[i] - t;
if (i < B.X.size()) t -= B.X[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
X = C;
neg = Cneg;
}
void getINT(){
//輸入
X.clear();
string a;cin>>a;
if (a[0] == '-') {
neg = true;
for (int i = a.size() - 1; i >= 1; i -- ) X.push_back(a[i] - '0');
}
else {
neg = false;
for (int i = a.size() - 1; i >= 0; i -- ) X.push_back(a[i] - '0');
}
}
void putINT(){
//輸出
if(neg) cout<<'-';
for (int i = X.size() - 1; i >= 0; i -- ) cout << X[i];
cout<<endl;
}
};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int T;cin>>T;
while(T--){
int n;cin>>n;
INT N,M;
N.getINT();
if(N.X[N.X.size()-1]==9){
FOR(i,1,N.X.size()+1) M.X.push_back(1);
}
else{
FOR(i,1,N.X.size()) M.X.push_back(9);
}
M.sub(N);
M.putINT();
}
return 0;
}
边栏推荐
- Simple and not simple programming language go
- Is it safe for tonghuashun to open an account? Is tongfushun a securities company?
- There is no CUDA option in vs2019+cuda11.1 new project
- Analysis of quantum secure communication
- Win11系统.NET Framework 3.5怎么启用?
- 网络可信身份认证
- CVPR 2022 | 提高小数据集利用效率,复旦等提出分层级联ViT网络
- 【MT2126】 数字游戏
- Responsive dream weaving template home furniture building materials website
- openGauss 联合产业界创新,共建开源数据库根社区
猜你喜欢

迪赛智慧数——折(渐变堆叠图):全国居民人均可支配收入

AIRIOT答疑第4期|如何使用数据分析引擎?

海量遥感数据处理与GEE云计算技术实践应用

易基因|ENCODE组蛋白ChIP-seq和转录因子ChIP-seq数据标准及处理流程

Easy gene encode histone chip SEQ and transcription factor chip SEQ data standard and processing flow

BTC与科技股相关性减弱 市场不确定性增强?53%投资者将转向另类资产

Simple and not simple programming language go

CVPR 2022 | 提高小数据集利用效率,复旦等提出分层级联ViT网络

AIRIOT答疑第4期|如何使用數據分析引擎?

Help brand insight -- Analysis of consumers' emotional behavior
随机推荐
腾势全新豪华中大型MPV曝光,安全、舒适一个不落
去除列表中重复元素 max min key
【ARCGIS创建中国南海诸岛及九段线小图框】
6-Redis架构设计到使用场景-持久化机制、缓存失效策略、缓存命中率
Without training code, the reasoning performance is improved by 1.4 ~ 7.1 times, and the industry's first automatic model compression tool is open source!
助力品牌洞察——消费者情绪行为分析
4-Redis架构设计到使用场景-string、list、set、zset、hash使用场景
WTK6900H语音识别单芯片实现智能语音识别蓝牙耳机方案设计
edusoho企培版 外部资讯抓取 有时无法获取图片问题/无法获取微信公众号图片
Hcip day 8 notes
一种指纹生物特征的隐私保护和认证方法
目前网上开户炒股安全吗?
[mt2109] rectangle
internship:移动端源码的分析
Extension and expansion of steam classroom education concept
5个时间序列预测的深度学习模型对比总结:从模拟统计模型到可以预训练的无监督模型
设计稳定的微服务系统时不得不考虑的场景
Interpreting the teaching principles of robot programming course
PolarDB for PostgreSQL的HTAP(Hybrid Transaction and
论软件自动化测试中 QR_Code 的登录的逻辑