Little X has met the following problem recently.
Let's define f(x) as the sum of digits in decimal representation of number x (for example, f(1234) = 1 + 2 + 3 + 4). You are to calculate
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:
ans = solve(l, r) % a; if (ans <= 0) ans += a;This code will fail only on the test with

The first line contains a single integer a (1 ≤ a ≤ 1018).
Print two integers: l, r (1 ≤ l ≤ r < 10200) — the required test data. Leading zeros aren't allowed. It's guaranteed that the solution exists.
46
1 10
126444381000032
2333333 2333333333333
题意:
定义f(x)为x在十进制下的各位数字之和。
给定a,需要你给出一个l,r(1 <= l <= r <= 1e200)使得(f(l) + ... +f(r))能被a整除。
题目确保有解。
(1 <= a <= 1e18)
题解:
姿势1:[一套尺取+二分连招]
令s(x) = f(1)+f(2)+…+f(x)。
我们只需找到s(R)%a = s(L-1)%a就美滋滋了
我们使用二分查询找到最小的且满足g(R)>=a
的值R, 接下来令L=1,然后跑一个双指针,调整
L,R的值即可。至于s(x)怎么求,我们把每一个
数位对答案的贡献加起来即可。
姿势2:[构造]
注意到f(x+1e18)=f(x)+1。
故s(x+1e18)=s(1e18)+s(x)+x。
即s(x+1e18)-s(x) = s(1e18)+x
让 s(1e18)+x%a==0,x= a-s(1e18)%a
L = a-s(1e18)%a+1,R = L + 1e18- 1
于是一组合法解就神不知鬼不知觉地构造出来了
代码请有兴趣的读者自行完成~
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
ll get(ll x)
{
ll ans=0;
for(ll i=1;i<=x;i*=1ll*10)
{
int d=(x/i)%10;
ll q=x/(1ll*10*i);
ans+=i*q*45;
rep(j,1,d) ans+=j*i;
ll t=x-(x/i)*i;
ans+=(t+1)*d;
}
return ans;
}
ll get1(ll x)
{
ll ans=0;
while(x)
{
ans+=x%10;
x/=10;
}
return ans;
}
int main()
{
ll a;
scanf("%lld",&a);
ll l=0,r=1e17,ans=-1;
while(l<=r)
{
ll m=(l+r)/2;
if(get(m)>=a)
ans=m,r=m-1;
else l=m+1;
}
l=1,r=ans;
ll now=get(ans);
while(now!=a)
{
if(now>a) now-=get1(l++);
else if(now<a) now+=get1(++r);
}
printf("%lld %lld\n",l,r);
return 0;
}
方法2:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
ll get(ll x)
{
ll ans=0;
for(ll i=1;i<=x;i*=1ll*10)
{
int d=(x/i)%10;
ll q=x/(1ll*10*i);
ans+=i*q*45;
rep(j,1,d) ans+=j*i;
ll t=x-(x/i)*i;
ans+=(t+1)*d;
}
return ans;
}
int main()
{
ll a;
scanf("%lld",&a);
ll t=get(1e17)%a;
ll l=a-t+1,r=l+1e17-1;
printf("%lld %lld\n",l,r);
return 0;
}