Yurchiu's Blog

P1822 魔法指纹

Yurchiu 2020-02-22, 21:55:12 1.7k 隐藏左右两栏 展示左右两栏

题解P1822 魔法指纹。作为一道打表水紫题,值得一刷。

CODE

如下:

#include<bits/stdc++.h>
using namespace std;
long long l=0,r=0,ans=0,data[]=
/*以下高能*/



{0,2045,1044,750,357,316,316,358,749,1044,1474,866,1044,303,220,154,115,84,125,346,927,378,558,750,221,204,154,118,134,146,382,110,270,303,357,176,204,168,128,89,75,85,126,298,221,316,176,220,168,88,69,69,88,168,220,176,316,221,298,126,85,75,89,128,168,204,176,357,303,270,111,382,145,134,118,154,204,222,749,558,378,927,347,124,84,115,154,220,303,1044,866,2022,754,224,71,86,115,168,298,558,1474,626,558,118,81,51,33,19,30,316,1461,866,1044,303,220,154,115,84,125,346,927,110,558,94,221,96,77,48,32,26,60,74,270,116,66,95,204,118,83,55,29,50,126,118,77,50,76,113,168,57,43,44,88,108,79,40,41,44,91,98,85,41,89,69,81,34,23,30,21,80,67,136,145,74,64,51,21,22,24,35,53,489,347,75,61,51,24,13,28,172,79,1143,754,47,45,51,36,15,20,229,924,240,240,298,118,69,51,28,43,63,585,866,328,303,113,77,51,35,25,62,375,378,558,750,221,204,154,118,134,146,382,67,194,303,84,176,96,81,49,32,25,47,98,298,126,69,95,220,108,52,38,43,57,168,113,76,50,77,118,126,50,48,69,128,118,60,40,34,29,96,74,156,97,134,76,77,34,27,23,26,55,162,193,124,54,69,51,23,23,35,37,311,315,224,35,58,51,22,28,72,277,25,78,118,168,65,58,35,27,14,14,382,92,116,220,101,69,53,33,39,32,110,558,94,221,96,77,48,32,26,60,110,270,303,357,176,204,168,128,89,75,34,83,118,221,64,176,113,109,42,27,38,52,108,220,95,69,126,298,98,47,35,54,69,168,96,76,59,65,194,110,51,66,74,118,101,60,44,30,47,138,32,60,75,84,65,77,52,21,25,33,24,41,47,71,52,69,56,34,35,16,23,47,91,118,115,52,45,35,16,15,170,64,65,113,154,65,61,32,32,20,378,240,79,126,204,101,64,46,27,54,67,194,303,84,176,96,81,49,32,25,85,126,298,221,316,176,220,168,88,69,27,42,109,113,176,64,221,118,83,34,29,55,83,118,204,95,66,116,270,74,41,49,78,76,154,96,77,33,78,110,23,46,49,54,115,101,79,29,23,52,15,22,53,35,86,65,81,52,26,12,12,26,52,81,65,86,35,53,22,15,52,23,29,79,101,115,54,49,46,23,110,78,33,77,96,154,76,78,49,41,74,270,116,66,95,204,118,83,55,29,34,83,118,221,64,176,113,109,42,27,69,88,168,220,176,316,221,298,126,85,25,32,49,81,96,176,84,303,194,67,54,27,46,64,101,204,126,79,240,378,20,32,32,61,65,154,113,65,64,170,15,16,35,45,52,115,118,91,47,23,16,35,34,56,69,52,71,47,41,24,33,25,21,52,77,65,84,75,60,32,138,47,30,44,60,101,118,74,66,51,110,194,65,59,76,96,168,69,54,35,47,98,298,126,69,95,220,108,52,38,27,42,109,113,176,64,221,118,83,34,75,89,128,168,204,176,357,303,270,110,60,26,32,48,77,96,221,94,558,110,32,39,33,53,69,101,220,116,92,382,14,14,27,35,58,65,168,118,78,26,276,72,28,22,51,58,36,224,315,310,37,35,23,23,51,69,54,125,193,161,55,26,23,27,34,77,76,134,98,155,74,96,29,34,40,60,118,128,69,48,50,126,118,77,50,76,113,168,57,43,38,52,108,220,95,69,126,298,98,47,25,32,49,81,96,176,84,303,194,68,382,145,134,118,154,204,222,749,558,379,374,62,25,35,51,77,113,304,327,867,584,63,43,28,51,69,118,298,241,239,924,229,20,15,36,51,45,47,754,1143,79,172,28,13,24,51,61,76,346,489,53,35,24,22,21,51,64,74,145,136,67,80,21,30,23,34,81,69,89,41,85,98,91,44,41,40,79,108,88,44,43,57,168,113,76,50,77,118,126,50,29,55,83,118,204,95,66,116,270,74,60,26,32,48,77,96,221,94,558,110,927,347,124,84,115,154,220,303,1044,866,1461,316,30,19,33,51,81,118,558,626,3567,2257,88,6,20,33,35,54,314,2022,371,1006,134,9,9,36,53,50,192,927,57,72,84,14,11,24,48,78,98,382,46,41,23,18,9,21,56,83,69,75,50,83,52,27,21,23,52,109,57,69,44,88,108,79,40,41,44,91,98,85,35,54,69,168,96,76,59,65,194,110,54,27,46,64,101,204,126,79,240,379,374,62,25,35,51,77,113,304,327,866,2022,754,224,71,86,115,168,298,558,1474};



long long magic(long long a)
{
	int num[200],ans[200],len=0;
	for(int i=1;a;i++)
		num[++len]=a%10,a/=10,ans[i]=num[i];
	if(len==1)
		return num[1];
	while(len>1)
	{
		for(int i=1;i<=len-1;i++)
			ans[i]=abs(ans[i+1]-ans[i]);
		len--;
		for(int i=len;i>=1;i--)
			if(ans[i]!=0)
				break;
			else
				len--;
	}
	return ans[1];
}
int main()
{ 
	scanf("%lld%lld",&l,&r);
	for(long long i=l;i<=r;)
		if(i%1000000==1&&i+1000000<=r)
			ans+=data[i/1000000+1],i+=1000000;
		else
			ans+=(magic(i)==7),i++;
	printf("%lld\n",ans);
	return 0;
}

思路

先上朴素

#include<bits/stdc++.h>
using namespace std;
long long l,r,data=0;
long long magic(long long a)
{
	int num[200],ans[200],len=0;
	for(int i=1;a;i++)
		num[++len]=a%10,a/=10,ans[i]=num[i];//分解
	if(len==1)
		return num[1];//特判
	while(len>1)
	{
		for(int i=1;i<=len-1;i++)
			ans[i]=abs(ans[i+1]-ans[i]);//做差
		len--;//长度减一
		for(int i=len;i>=1;i--)
			if(ans[i]!=0)
				break;
			else
				len--;//高位去0
	}
	return ans[1];//最后一个数
}
int main()
{ 
	scanf("%lld%lld",&l,&r);
	for(long long i=l;i<=r;i++)
		data+=(magic(i)==7);//题意
	printf("%lld\n",data);
	return 0;
}

俗话说,艰苦朴素永不忘嘛。

当然,肯定超时(不过数据水,应该只得30分,却得了60分!!!)。

考虑打表

思路:数据范围1110000000001000000000,把这个范围的数分为10001000个区间,每个区间分别是[1,1000000][1,1000000][1000001,2000000][1000001,2000000],…,[999000001,1000000000][999000001,1000000000]的范围。

算出每个区间的符合题意的数的个数,存入data[]数组。

这样,从l到r的答案数就是把[l,r][l,r]中包含的区间所对应的data[]加上剩下的零余数。例子(极限数据,magic(a)返回aa的 magic 值是(11)否(00)等于77):

ans(2,999999999)=i=2999datai+i=21000000magic(i)+i=999000001999999999magic(i)ans(2,999999999)=\sum_{i=2}^{999}data_i+\sum_{i=2}^{1000000}magic(i)+\sum_{i=999000001}^{999999999}magic(i)

效率很高!

打表生成器:

#include<bits/stdc++.h>
using namespace std;
long long l,r,data=0;
long long magic(long long a)
{
	int num[200],ans[200],len=0;
	for(int i=1;a;i++)
		num[++len]=a%10,a/=10,ans[i]=num[i];//分解
	if(len==1)
		return num[1];//特判
	while(len>1)
	{
		for(int i=1;i<=len-1;i++)
			ans[i]=abs(ans[i+1]-ans[i]);//做差
		len--;//长度减一
		for(int i=len;i>=1;i--)
			if(ans[i]!=0)
				break;
			else
				len--;//高位去0
	}
	return ans[1];//最后一个数
}
int main()
{ 
    freopen("out.txt","w",stdout);
	for(long long i=1;i<=1000000000;i++)
    {
        data+=(magic(i)==7);
        if(i%1000000==0)
        {
            printf("%lld,",data);//方便复制
            data=0;
        }
    }
	return 0;
}

END

好了,现在只需把落在[l,r][l,r]区间的小区间的data[]值一加,再算剩下的,OK!





本文作者:Yurchiu

本文链接:https://yz-hs.github.io/dfc5f9f2632e/

版权声明:本博客中所有原创文章除特别声明外,均允许规范转载,转载请注明出处。所有非原创文章,按照原作者要求转载。


By Yurchiu.
其他物件杂物收纳
Hitokoto

Yurchiu 说,除了她以外的人都很强!嘤嘤嘤~~
博客信息
文章数目
158
最近更新
08-21
本站字数
350.6k
文章目录