HDU 4389 (分块打表)

题意:
f(x)表示x的各数位的和,给出[L,R],问区间中有多少个数x,存在x mod f(x) == 0


思路:
L,R范围在1e9,估算复杂度后发现应该可以递推打出一个表,按1000000每块,分块后将块内答案保存在数组中,块间累加,块内暴力,查询即可。(好像正解是数位dp)


代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const ll mod=998244353;
const double eps=1e-5;
const int maxn=1005;
const int maxm=20005;

int blk[1005]={0,95428,91133,87403,83959,80872,78193,75678,73361,71069,68999,91133,87229,83794,80647,77871,75372,73053,70979,68817,66865,87316,83715,80613,77667,75054,72815,70669,68679,66664,64937,83854,80541,77613,74904,72496,70351,68337,66550,64627,62903,80801,77692,74992,72392,70098,68035,66153,64475,62676,61051,78115,75169,72596,70145,67951,65961,64179,62610,60876,59365,75578,72881,70462,68064,65974,64130,62390,60858,59297,57844,73360,70757,68420,66223,64259,62381,60730,59331,57833,56422,71063,68679,66522,64368,62583,60861,59255,57837,56428,55186,68994,66763,64744,62736,61035,59436,57882,56515,55192,54028,91149,87382,83943,80971,78121,75542,73287,71083,68920,66940,87201,83826,80643,77873,75336,72979,70823,68850,66896,64985,83750,80545,77592,75078,72734,70527,68563,66703,64895,63120,80543,77606,74810,72434,70251,68183,66362,64659,62904,61293,77785,74976,72365,70067,67986,66044,64339,62693,61078,59591,75282,72684,70074,67901,65972,64123,62418,60890,59434,57991,72964,70459,68075,65946,64072,62303,60758,59247,57802,56526,70858,68517,66202,64215,62460,60727,59228,57815,56446,55208,68777,66565,64413,62522,60852,59257,57819,56441,55159,54011,66818,64777,62693,60962,59435,57860,56539,55257,54020,52889,87424,83970,80928,78206,75593,73237,70998,68983,66926,65072,83794,80602,77882,75381,72946,70811,68760,66875,65006,63292,80593,77603,75035,72746,70517,68517,66624,64908,63132,61571,77603,74810,72413,70201,68125,66304,64506,62941,61321,59835,75083,72351,70033,68016,66003,64187,62549,61092,59561,58186,72718,70174,67969,65938,64090,62382,60761,59396,58039,56747,70605,68125,66007,64130,62326,60720,59191,57829,56531,55366,68624,66305,64258,62440,60789,59255,57746,56482,55261,54089,66675,64422,62511,60847,59248,57823,56460,55157,54015,52981,64839,62796,60971,59324,57879,56560,55229,54021,52911,51962,83939,80968,78242,75664,73285,71005,68927,66965,65102,63255,80628,77867,75381,73029,70816,68733,66827,65007,63304,61601,77650,75084,72718,70537,68535,66581,64813,63194,61607,59976,74875,72436,70238,68164,66239,64478,62874,61296,59840,58432,72453,70138,68025,65988,64225,62512,60982,59591,58193,56886,70257,68015,66006,64117,62381,60795,59309,57966,56705,55466,68280,66114,64189,62393,60751,59158,57830,56551,55300,54133,66394,64366,62598,60835,59268,57819,56512,55294,54121,53021,64617,62609,60869,59298,57862,56414,55171,54122,53018,51888,62905,61038,59394,57869,56533,55201,54029,52960,51975,50890,80876,78206,75674,73356,71048,69000,66963,65128,63304,61563,77798,75319,72987,70897,68720,66813,65057,63376,61580,59991,74983,72684,70518,68529,66555,64799,63111,61630,60054,58551,72388,70177,68089,66282,64431,62766,61247,59903,58413,57063,70089,67990,66000,64262,62511,60930,59492,58282,56924,55620,67994,65966,64097,62420,60759,59297,57938,56715,55497,54366,66104,64169,62305,60692,59232,57804,56470,55375,54213,53126,64405,62518,60787,59265,57774,56466,55251,54206,53096,52038,62649,60900,59233,57783,56434,55168,54053,53038,52004,51021,61064,59428,57884,56420,55169,54017,52931,51965,50938,50040,78186,75619,73366,71160,69014,67021,65168,63271,61531,59789,75321,72997,70859,68820,66872,65023,63290,61655,60026,58338,72709,70465,68512,66620,64791,63120,61564,60056,58576,57008,70189,68095,66219,64456,62743,61200,59790,58399,57035,55662,67976,66002,64255,62514,60935,59514,58169,56884,55665,54366,66020,64058,62386,60813,59308,57910,56692,55549,54343,53141,64176,62331,60753,59207,57774,56503,55346,54240,53180,51965,62541,60771,59243,57798,56432,55206,54179,53141,52126,50984,60931,59270,57780,56365,55163,53980,52934,52043,51084,49969,59469,57851,56429,55151,53949,52883,51902,50962,50083,49003,75669,73314,71132,69192,67076,65179,63329,61611,59742,58177,72953,70827,68786,66968,65058,63309,61595,60074,58364,56852,70510,68497,66611,64918,63163,61541,59987,58579,57046,55678,68143,66239,64427,62826,61266,59773,58314,57103,55681,54429,66050,64235,62523,61002,59508,58191,56836,55658,54369,53252,64117,62412,60770,59300,57955,56708,55461,54358,53168,52081,62393,60772,59200,57773,56509,55376,54196,53166,52062,51026,60834,59269,57782,56448,55209,54150,53094,52157,50962,50000,59244,57795,56424,55117,53907,52989,51981,51000,49996,48999,57858,56492,55122,53906,52858,51853,50908,49995,48973,48028,73376,71193,69151,67235,65327,63422,61643,59811,58238,56819,70852,68857,66977,65169,63436,61696,60049,58385,56926,55608,68548,66619,64904,63257,61635,60017,58542,57042,55637,54449,66238,64482,62866,61329,59839,58406,57042,55637,54417,53270,64287,62603,61076,59613,58266,56915,55697,54407,53220,52189,62438,60794,59371,58046,56756,55501,54373,53258,52129,51035,60785,59206,57844,56601,55389,54209,53201,52069,51004,50024,59287,57838,56487,55293,54217,53105,52079,51063,50098,49001,57844,56410,55172,54039,53049,51968,51020,50007,49061,48097,56523,55167,53948,52865,51930,50920,49979,49005,48062,47119,71088,69030,67073,65320,63418,61576,59762,58228,56791,55387,68754,66855,65084,63450,61653,60018,58321,56877,55571,54307,66542,64838,63163,61663,60043,58509,56936,55628,54464,53235,64424,62790,61214,59836,58405,56993,55536,54338,53229,52134,62566,61019,59582,58263,56890,55668,54353,53228,52143,51069,60812,59387,58011,56769,55513,54396,53177,52069,51101,50038,59254,57842,56558,55409,54253,53173,52064,51050,49999,48992,57793,56463,55285,54190,53098,52090,51019,50039,49022,48051,56396,55137,54072,53041,51979,51042,49997,49020,48059,47057,55195,53979,52866,51890,50943,50026,48963,48044,47146,46133,68971,67021,65187,63386,61584,59807,58191,56745,55401,54211,66812,65029,63342,61684,60024,58323,56831,55503,54225,53119,64786,63079,61587,60081,58510,56930,55589,54395,53153,52122,62783,61186,59801,58428,56972,55570,54286,53213,52071,51059,61035,59574,58222,56942,55677,54320,53159,52125,51060,50042,59401,57959,56746,55540,54383,53127,52034,51086,50033,49111,57907,56561,55381,54322,53234,52048,50930,50036,49010,48097,56485,55307,54147,53105,52152,51036,49942,49005,48094,47188,55188,54036,52998,51994,51085,49965,48966,48083,47053,46227,53992,52925,51929,50871,50002,49014,47998,47107,46136,45281};
int get(int now){
    if(now==0)return 0;
    return get(now/10)+now%10;
}
int getpre(int now){
    int res=0;
    int mark;
    for(int i=1000000;;i+=1000000){
        if(i<=now)res+=blk[i/1000000];
        else {
            mark=i-1000000;
            break;
        }
    }
    for(int i=mark+1;i<=now;i++){
        if(i%get(i)==0)res++;
    }
    return res;
}
void solve() {
    int t;
    sd(t);
    for(int kase=1;kase<=t;kase++){
        int l,r;
        sdd(l,r);
        printf("Case %d: %d\n",kase,getpre(r)-getpre(l-1));
    }
    return ;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#else
    //    freopen("","r",stdin);
    //    freopen("","w",stdout);
#endif
    solve();
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值