题解 CF817C 【Really Big Numbers】

题目链接:CF817C

前置算法 : 二分

我们先考虑如何得到答案,若最小满足\(x\)减去其各数位之和后大于\(s\)

\(ans = n - x + 1\)

我们只要打个表就可以发现\(:\)

若\(x < y\)则\(|x| \leq |y|\) \((\)设\(|x|\)表示\(x\)减去其各数位之和\()\)

证明就不写了

说明答案是递增的, 那就用二分

我们二分出最小满足\(|x|\)大于\(s\)

\(check\)函数就根据题意所写

如果找不到\(|x| \leq s\)就输出\(0\)

时间复杂度:\(O(log_2n \times log_{10}n)\)

\(Code:\)

#include <bits/stdc++.h>

#define ull unsigned long long

using namespace std;

ull Left, Right;
ull n, s, ans = 0x7fffffff / 3;

inline int check(ull mid) {
    ull now = mid;
    while (mid) {
        now -= mid % 10;
        mid /= 10;
    }
    return now >= s;
}

int main() {
    cin >> n >> s;
    Right = n;
    while (Right >= Left) {
        ull mid = Left + Right >> 1;
        if (check(mid)) {
            ans = mid;
            Right = mid - 1;
        }
        else Left = mid + 1;
    }
    printf("%lld", ans == 0x7fffffff / 3 ? 0 : n - ans + 1);
    return 0;
}

原文地址:https://www.cnblogs.com/chz-hc/p/12221319.html

时间: 2024-10-11 19:48:20

题解 CF817C 【Really Big Numbers】的相关文章

题解[CodeForces1154A]Restoring Three Numbers

Description Polycarp has guessed three positive integers \(a\), \(b\) and \(c\). He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order - their pairwise sums (three numbers) and sum of all three numbers (one n

CF817C Really Big Numbers

思路: 二分. 实现: 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 typedef long long ll; 6 7 const ll MAXN = 1e18; 8 9 ll n, s; 10 11 bool check(ll x) 12 { 13 ll sum = 0; 14 ll tmp = x; 15 while (x) 16 { 17 sum += x % 10; 18 x /=

LeetCode题解之Find All Numbers Disappeared in an Array

1.题目描述 2.问题分析 使的 A[i] = i+1 ,最后检查不满足这个条件的i+1 .即为缺失的值. 3.代码 1 vector<int> findDisappearedNumbers(vector<int>& nums) { 2 for( int i = 0; i < nums.size() ; ++i ){ 3 if( nums[i] != nums[ nums[i] - 1 ] ){ 4 swap( nums[i] , nums[nums[i] - 1]

题解 CF47A 【Triangular numbers】

这题其实就是高斯求和问题,即1+...+x=x(x+1)/2. 由此,我们就可以用递推的思想来解决问题: include<bits/stdc++.h> using namespace std; int main() { //freopen("ask.in","r",stdin); //freopen("ask.out","w",stdout); long long n,r=0; cin>>n; for(

LeetCode: Add Two Numbers 题解

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【优先队列基础练习】POJ1338 Uva136 - Ugly Numbers题解

昨天夜里学了栈.队列和优先队列,栈还有的地方不大懂,队列基本可以,优先队列很高兴.本想今天上午继续弄这个,但是跟着李晨他们一块听了清华交院罗雨屏老师讲的计算几何= =虽然以我的水平听计算几何有点早,但至少是听懂了很多,感觉很高兴:十点多就没有再听,毕竟之后越来越深亚历山大.于是就来写一下丑数的题解. 题目:除2,3,5外不能被其他素数整除的数为丑数,求第1500个丑数(Uva版),求第n(n<=1500)个丑数(POJ版) 思路:第一个是1,放入升序优先队列(小的先出),之后每次出队第一个元素,

POJ 3252 Round Numbers 数学题解

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

【题解】【数组】【Prime and composite numbers】【Codility】Flags

A non-empty zero-indexed array A consisting of N integers is given. A?peak?is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N ? 1 and A[P ? 1] < A[P] > A[P + 1]. For example, the follo

【题解】【数组】【Prime and composite numbers】【Codility】Peaks

A non-empty zero-indexed array A consisting of N integers is given. A?peak?is an array element which is larger than its neighbors. More precisely, it is an index P such that 0 < P < N ? 1,? A[P ? 1] < A[P] and A[P] > A[P + 1]. For example, the