ZZNUOJ-2155-单身man集合-【标程做法:数位DP-1-10^8,提前暴力打表法: 砍时间复杂度到10^5】
ZZNUOJ-2155: 单身MAN集合
1 题目描述: 2 单身man们突然集结起来了,虽然我们不知道它们想要干什么。你作为单身man的首领需要管理好每一只单身man,机智的你给每一只单身man编了一个编号。但是单身man们不喜欢相邻的两个数字是相同数字(例如 11,122, 1223等),单身man们也不喜欢一个数字的左右两边的数字是相同数字(例如 121,2323等),因为它们觉得中间的数字太可怜了。 3 作为单身man的首领的你不想刺激这些单身man,决定把这些编号里面含有7或者各位数字之和为7的倍数编号去掉,免得单身man们收到刺激。 4 5 输入 6 7 输入一个整数T (1 <= T <= 10)开始,表示测试用例的数量。 8 每一行输入一个整数 l ,r (1<= l <= r <= 100000000)表示编号的范围。 9 10 输出 11 12 在 l 到 r 的范围里面最多有多少个可用编号。 13 14 样例输入 15 16 1 17 1 10 18 19 样例输出 20 21 9
大致思路:
求区间【L,R】的结果,可以转化成【1,R】-【1,L-1】的结果,自行画图即可!
提前存一些节点,后面计算的时候直接调用即可!
例如下面代码中的F[i] 表示X 整除100000后得到i,然后从1到 i*100000的结果是F[i].依次类推,这样每次跑的话,从1--i*100000的地方已经可以从F数组中O(1)的时间内调用出来!只需要跑i*100000+1到X的结果了!(针对每组数据,时间复杂度不超过10^5)
打表代码:
然后找到与main.cpp同目录级别的output.txt,用记事本打开即可!然后把这些结果全部存进你的另一个工程的F[ ]数组中!
1 #include <iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<string> 5 #include<algorithm> 6 #include<queue> 7 #define ll long long 8 using namespace std; 9 #include<math.h> 10 #define N 100008 11 #define ll long long 12 #define inf 0x3f3f3f3f 13 int F[10000]; 14 15 bool judge(int x) 16 { 17 int a[10]= {0}; 18 int len=0,s=0; 19 while(x>0) 20 { 21 a[++len]=x%10; 22 x/=10; 23 if(a[len]==7)return false; 24 if(len>=2&&a[len]==a[len-1])return false; 25 if(len>=3&&a[len]==a[len-2])return false; 26 s+=a[len]; 27 } 28 if(s%7!=0) 29 return true; 30 return false; 31 } 32 33 int main(){ 34 freopen("output.txt","w",stdout); 35 36 int s=0; 37 int n=100000000; 38 for(int i=1;i<=n;i++){ 39 if(judge(i)) 40 s++; 41 if(i%100000==0) 42 F[i/100000]=s; 43 } 44 cout<<"{0"; 45 for(int i=1;i<=1000;i++) 46 printf(",%d",F[i]); 47 cout<<"}"; 48 49 return 0; 50 }
View Code
题解代码:
记得加上F[N]数组!
1 #include <iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<math.h> 8 #include<stack> 9 #define ll long long 10 using namespace std; 11 const double eps=1e-7; 12 #define PI acos(-1.0) 13 #define N 100008 14 int F[2000]= {0,21956,38420,54884,71348,87792,104256,120724,120724,137188, 15 153652,168058,168058,182464,196869,211279,225661,240087,240087,254494,268900, 16 283305,297711,297711,312117,326543,340925,355335,355335,369741,384148,398548,412954,427360,427360,441763,456157,470552,470552,484958,499364,513784,528191,542605,556990,556990,571396,585773,585773,600180,614594,628982,643378,657774,672162,686560,686560,700958,700958,715354,729750,744135,758549,772956,787376,801753,816159,816159,816159,830573,844980,844980,844980,844980,844980,844980,844980,844980,844980,844980,844980,859386,873793,888199,902604,917014,931396,945822,945822,945822,960228,974633,989039,1003446,1017852,1032278,1046660,1061070,1061070,1075476,1075476,1075476,1075476,1089881,1104291,1118696,1133072,1147507,1147507,1161912,1176317,1176317,1176317,1176317,1176317,1176317,1176317,1176317,1176317,1176317,1176317,1190720,1190720,1190720,1205142,1219552,1233958,1248348,1248348,1262754,1277159,1291590,1291590,1306004,1306004,1320416,1334833,1349225,1349225,1363631,1378045,1392437,1392437,1406844,1421245,1421245,1435680,1450072,1450072,1464486,1478893,1493274,1493274,1507680,1522110,1536510,1536510,1550925,1550925,1565324,1579730,1594157,1594157,1608556,1622959,1637347,1651755,1651755,1651755,1666161,1680560,1680560,1680560,1680560,1680560,1680560,1680560,1680560,1680560,1680560,1680560,1694962,1694962,1709368,1723774,1738200,1752589,1766996,1766996,1766996,1781402,1795805,1795805,1810210,1824632,1839042,1853448,1867838,1867838,1882244,1882244,1882244,1896651,1896651,1911054,1925476,1939864,1954271,1954271,1968678,1983092,1997501,1997501,1997501,2011923,2026330,2040736,2055122,2055122,2069528,2083934,2083934,2083934,2083934,2083934,2083934,2083934,2083934,2083934,2083934,2083934,2098327,2112741,2112741,2112741,2127157,2141602,2156002,2156002,2170416,2184815,2199212,2213618,2213618,2228049,2228049,2242458,2256873,2256873,2271279,2285693,2300101,2314507,2314507,2328930,2343321,2343321,2357723,2357723,2372129,2386535,2400943,2415340,2415340,2429738,2444153,2458534,2458534,2458534,2472931,2487328,2487328,2487328,2487328,2487328,2487328,2487328,2487328,2487328,2487328,2487328,2501737,2516143,2516143,2530565,2544972,2559378,2573764,2573764,2573764,2588170,2602596,2617002,2617002,2631392,2645813,2660220,2674606,2674606,2689012,2689012,2689012,2703426,2717826,2717826,2732242,2746654,2761036,2761036,2775450,2789850,2804276,2804276,2818682,2818682,2833100,2847510,2861900,2861900,2876306,2890712,2905106,2919512,2919512,2919512,2933914,2948356,2962761,2962761,2977167,2991573,2991573,2991573,2991573,2991573,2991573,2991573,2991573,2991573,2991573,2991573,3005998,3020411,3034816,3034816,3034816,3049226,3063647,3063647,3078060,3092465,3106871,3121278,3135700,3135700,3150115,3150115,3164536,3164536,3178943,3193365,3207757,3222156,3236562,3236562,3250996,3265389,3265389,3265389,3279788,3294194,3294194,3294194,3294194,3294194,3294194,3294194,3294194,3294194,3294194,3294194,3308620,3323026,3337432,3337432,3351850,3366260,3380650,3380650,3380650,3395056,3409450,3423856,3438262,3438262,3452664,3467106,3481511,3481511,3495917,3495917,3495917,3510314,3524711,3539104,3539104,3553514,3567901,3567901,3582298,3596695,3611082,3611082,3625488,3639882,3639882,3654324,3668726,3668726,3683132,3697538,3711924,3726330,3726330,3740756,3740756,3755166,3769584,3769584,3783990,3798396,3812808,3827208,3841622,3841622,3841622,3856034,3870450,3870450,3884850,3899264,3899264,3899264,3899264,3899264,3899264,3899264,3899264,3899264,3899264,3899264,3913670,3928092,3942499,3956905,3956905,3956905,3971320,3971320,3985742,4000149,4014547,4028952,4043365,4057790,4057790,4072200,4072200,4072200,4086605,4101018,4101018,4101018,4101018,4101018,4101018,4101018,4101018,4101018,4101018,4101018,4115405,4129811,4144217,4158611,4158611,4173053,4187455,4187455,4187455,4201861,4216247,4230653,4245059,4259485,4259485,4273895,4288313,4288313,4302719,4302719,4302719,4317118,4331532,4345925,4360325,4360325,4374741,4374741,4389140,4403554,4417944,4417944,4432350,4446776,4461162,4461162,4475583,4475583,4489989,4504395,4518817,4533223,4533223,4547632,4562018,4562018,4576425,4576425,4590831,4605237,4619640,4634054,4648461,4648461,4662868,4662868,4677290,4677290,4691704,4706111,4720509,4734906,4749303,4763711,4763711,4763711,4778126,4778126,4792523,4806920,4806920,4806920,4806920,4806920,4806920,4806920,4806920,4806920,4806920,4806920,4821351,4835765,4850171,4864568,4878983,4878983,4878983,4878983,4893397,4907803,4907803,4907803,4907803,4907803,4907803,4907803,4907803,4907803,4907803,4907803,4922193,4936599,4951005,4965431,4979817,4979817,4994238,4994238,4994238,5008644,5023066,5037472,5051878,5066287,5080673,5080673,5095080,5095080,5109486,5109486,5109486,5123900,5138306,5152737,5167129,5181546,5181546,5181546,5195960,5210366,5224788,5224788,5239194,5253597,5267987,5282393,5282393,5282393,5296798,5311204,5325610,5340016,5340016,5354418,5368825,5383214,5383214,5383214,5397620,5412026,5426436,5440841,5455246,5455246,5469681,5484057,5484057,5484057,5498462,5512867,5527270,5541669,5556075,5570502,5570502,5584910,5584910,5584910,5599309,5613715,5628145,5642551,5656950,5671331,5685746,5685746,5685746,5685746,5700152,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5714551,5728973,5743378,5757784,5772187,5786577,5800983,5800983,5800983,5800983,5815389,5829795,5844201,5858607,5873009,5887416,5901805,5901805,5901805,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5916211,5930616,5945021,5959431,5973836,5988212,6002647,6002647,6002647,6017052,6031454,6031454,6045860,6060266,6074692,6089081,6103488,6103488,6103488,6117894,6132297,6146703,6146703,6161125,6175535,6189941,6204331,6204331,6204331,6218736,6233167,6247573,6261987,6261987,6276399,6290816,6305208,6305208,6305208,6319622,6334014,6348428,6362835,6377236,6377236,6391671,6406063,6406063,6406063,6420470,6434851,6449250,6463656,6478086,6492486,6492486,6506901,6506901,6506901,6521307,6535734,6550140,6564539,6578942,6593330,6607738,6607738,6607738,6607738,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6622137,6636540,6650946,6665351,6679773,6694183,6708589,6722979,6722979,6722979,6722979,6722979,6737386,6751800,6766203,6780625,6795013,6809420,6809420,6823827,6823827,6838236,6838236,6852642,6867064,6881471,6895877,6910263,6910263,6924669,6924669,6939095,6953501,6953501,6967891,6982312,6996719,7011105,7011105,7025511,7025511,7039904,7054318,7068717,7068717,7083133,7097578,7111978,7111978,7126392,7126392,7140789,7155195,7169609,7184040,7184040,7198449,7212864,7212864,7227270,7227270,7241678,7256084,7270490,7284913,7299304,7299304,7313706,7313706,7328112,7328112,7342520,7356917,7371314,7385712,7400127,7414508,7414508,7414508,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7428905,7443314,7457720,7472126,7486548,7500955,7515361,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747,7529747 17 }; 18 19 20 /****/ 21 bool judge(int x) 22 { 23 int a[10]= {0}; 24 int len=0,s=0; 25 while(x>0) 26 { 27 a[++len]=x%10; 28 x/=10; 29 if(a[len]==7)return false; 30 if(len>=2&&a[len]==a[len-1])return false; 31 if(len>=3&&a[len]==a[len-2])return false; 32 s+=a[len]; 33 } 34 if(s%7!=0) 35 return true; 36 return false; 37 } 38 int fact(int l,int r) 39 { 40 int sum=0; 41 for(int i=l; i<=r; i++) 42 { 43 if(judge(i)) 44 sum++; 45 } 46 return sum; 47 } 48 int solve(int x) //从[1--x]的闭区间的包含的数 49 { 50 int id1=x/100000; 51 return F[id1]+fact(id1*100000+1,x ); 52 } 53 int main() 54 { 55 int T,l,r; 56 scanf("%d",&T); 57 while(T--) 58 { 59 scanf("%d%d",&l,&r); 60 // cout<<"***"<<fact(l,r)<<"****"; 61 if(r<100000) 62 { 63 printf("%d\n",fact(l,r)); 64 } 65 else 66 { 67 printf("%d\n",solve(r)-solve(l-1)); 68 // cout<<"r: "<<solve(r)<<endl; 69 // cout<<"l: "<<solve(l-1)<<endl; 70 } 71 } 72 73 return 0; 74 } 75 76 77 /************************************************************** 78 Problem: 2155 79 User: 137666 80 Language: C++ 81 Result: 正确 82 Time:8 ms 83 Memory:2032 kb 84 ****************************************************************/
View Code(灰常简单的代码)
你不逼自己一把,你永远都不知道自己有多优秀!只有经历了一些事,你才会懂得好好珍惜眼前的时光!