题目连接:
https://vjudge.net/contest/285797#problem/F
预处理一百万个数据,之后问的时候进行相加就行了。原先以为开一个arr[1000000][30]的数组会爆内存。没想到居然不会爆。
代码:
#include <iostream>
#include <cmath>
using namespace std;
int flag[1000001][30];
const int inf=1e6;
int nownow[30];
int fun(int x)
{
int ans=0;
while(x>0)
{
ans+=(x%10);
x=x/10;
}
return ans;
}
int main()
{
int temp=0;
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
{
temp=abs( fun(i)-fun(j) );
nownow[temp]++;
flag[ i*1000+j ][ temp ]=nownow[temp];
}
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
int now=abs( fun(temp%1000)-fun(temp/1000) );
int ans=0;
for(int j=0;j<now;j++)
{
int thenow=temp;
while( flag[thenow][j]==0 )thenow-- ;
ans+=flag[thenow][j];
}
printf("%d\n",ans);
}
return 0;
}