Codeforces Round #268 (Div. 1) -C Hack it

本文深入解析了一道Codeforces上的构造题,通过巧妙的方法从一个固定位置进行转移,利用数位和的特性,找到了满足条件的连续区间。文章详细介绍了如何构造一个大的区间并计算其数位和,进而解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces传送门
洛谷传送门

题目描述

Little X has met the following problem recently.

Let’s define f(x) f ( x ) as the sum of digits in decimal representation of number x x (for example, f(1234)=1+2+3+4 ). You are to calculate img

Of course Little X has solved this problem quickly, has locked it, and then has tried to hack others. He has seen the following C++ code:

<br></br> ans = solve(l, r) % a;<br></br> if (ans \leq  0)<br></br> ans += a;<br></br><br></br>


ans = solve(l, r) % a;
if (ans \leq 0)
ans += a;

This code will fail only on the test with img. You are given number a a , help Little X to find a proper test for hack.

输入输出格式

输入格式:

The first line contains a single integer a(1a1018) .

Print two integers: l,r(1lr<10200) l , r ( 1   ≤   l   ≤   r   <   10 200 ) — the required test data. Leading zeros aren’t allowed. It’s guaranteed that the solution exists.

输入输出样例

输入样例#1:
46
输出样例#1:
1 10
输入样例#2:
126444381000032
输出样例#2:
2333333 2333333333333

题目大意

给你一个数字 a a , 要求找到一段连续区间[l,r],使得区间中所有数的数位和为 a a 的倍数。 lr<10200

解题分析

真是毒瘤构造题专场…这场div1前三道都是构造…

直接构造是很难的, 我们想办法从一个固定的位置转移。

然后我们就能发现对于一个区间 [1,10n] [ 1 , 10 n ] , 如果左右端点同时 +n + n , 其数位和也同时 +n + n 。 这样我们就可以先得到一个较大的区间并计算其数位和, 再加上左右端点一段即可。

对于 [1,10n] [ 1 , 10 n ] , 其数位和为 10n1×n×45+1 10 n − 1 × n × 45 + 1

代码如下(贴了一个高精的板QAQ):

#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#define R register
using namespace std;
namespace BigInteger
{
#define maxn 10005
struct Big_integer{  
    int d[maxn], len;  

    void clean() { while(len > 1 && !d[len-1]) len--; }  

    Big_integer()          { memset(d, 0, sizeof(d)); len = 1; }  
    Big_integer(int num)   { *this = num; }   
    Big_integer(char* num) { *this = num; }  
    Big_integer operator = (const char* num){  
        memset(d, 0, sizeof(d)); len = strlen(num);  
        for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';  
        clean();  
        return *this;  
    }  

    Big_integer operator = (int num){  
        char s[10005]; sprintf(s, "%d", num);  
        *this = s;  
        return *this;  
    }  

    Big_integer operator + (const Big_integer& b){  
        Big_integer c = *this; int i;  
        for (i = 0; i < b.len; i++){  
            c.d[i] += b.d[i];  
            if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;  
        }  
        while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;  
        c.len = max(len, b.len);  
        if (c.d[i] && c.len <= i) c.len = i+1;  
        return c;  
    }  

    Big_integer operator - (const Big_integer& b){  
        Big_integer c = *this; int i;  
        for (i = 0; i < b.len; i++){  
            c.d[i] -= b.d[i];  
            if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;  
        }  
        while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;  
        c.clean();  
        return c;  
    }  

    Big_integer operator * (const Big_integer& b)const{  
        int i, j; Big_integer c; c.len = len + b.len;   
        for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)   
            c.d[i+j] += d[i] * b.d[j];  
        for(i = 0; i < c.len-1; i++)  
            c.d[i+1] += c.d[i]/10, c.d[i] %= 10;  
        c.clean();  
        return c;  
    }  

    Big_integer operator / (const Big_integer& b){  
        int i, j;  
        Big_integer c = *this, a = 0;  
        for (i = len - 1; i >= 0; i--)  
        {  
            a = a*10 + d[i];  
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  
            c.d[i] = j;  
            a = a - b*j;  
        }  
        c.clean();  
        return c;  
    }  

    Big_integer operator % (const Big_integer& b){  
        int i, j;  
        Big_integer a = 0;  
        for (i = len - 1; i >= 0; i--)  
        {  
            a = a*10 + d[i];  
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  
            a = a - b*j;  
        }  
        return a;  
    }  

    Big_integer operator += (const Big_integer& b){  
        *this = *this + b;  
        return *this;  
    }  

    bool operator <(const Big_integer& b) const{  
        if(len != b.len) return len < b.len;  
        for(int i = len-1; i >= 0; i--)  
            if(d[i] != b.d[i]) return d[i] < b.d[i];  
        return false;  
    }  
    bool operator >(const Big_integer& b) const{return b < *this;}  
    bool operator<=(const Big_integer& b) const{return !(b < *this);}  
    bool operator>=(const Big_integer& b) const{return !(*this < b);}  
    bool operator!=(const Big_integer& b) const{return b < *this || *this < b;}  
    bool operator==(const Big_integer& b) const{return !(b < *this) && !(b > *this);}  

    string str() const{  
        char s[maxn]={};  
        for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';  
        return s;  
    }  
};  

istream& operator >> (istream& in, Big_integer& x)  
{  
    string s;  
    in >> s;  
    x = s.c_str();  
    return in;  
}  

ostream& operator << (ostream& out, const Big_integer& x)  
{  
    out << x.str();  
    return out;  
}  
}
using namespace BigInteger;
Big_integer bg, ed, dat;
int main(void)
{
    cin >> dat; bg = 1;
    Big_integer base = 10, mul = 10;
    for (R int i = 1; i <= 18; ++i) base = base * mul;
    ed = base;//10 ^ 19
    mul = 45;
    base = 19;
    mul = mul * base;
    base = 10;
    for (R int i = 1; i <= 18; ++i) mul = mul * base;
    mul = mul + bg;
    Big_integer mod = mul % dat;
    Big_integer cha = dat - mod;
    cout << (bg + cha) << " " << (ed + cha);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值