跳到正文

题解

优秀的模拟

Aoki_灏约 2 分钟
原文
文章信息与版本

文章信息

发布
2018年7月29日
收录
2026年2月18日
更新
2026年11月6日
篇幅
约 2 分钟
题目
P1662

版本

  1. v2当前
  2. v1

看着这题并没有正解题解就来搬运一下xlj巨神的正解

然后就是在暴力的基础上优化

来自这位巨神

间隔打表大法好!!!

优化点

代码
     1.优化%7

      cnt计数器计算%7的值,每次加的时候就+1
      到了7就变0,这样减少算mod7的时间

    2.优化每位数上有7的情况

      每次加1的时候用类似高精度的方法统计每位上面是否有7
      具体来说
      记录一个全局变量N7
      就是每一位到了7的时候N7=N7+1
      到了8的时候N7=N7-1
      处理进位的情况
      只有N7等于0的时候方向才不变

     3.间隔打表
      对于10^8以上的数据
      从10^8到10^9,每隔10的八次方就算出来
      这样的话
      10^9的被压缩成10^8的复杂度就可以跑过了

代码

C++
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
int num[20];
int c=0;
int n;
inline void add(){
	num[1]++;
	if(num[1]==8) c--;
	if(num[1]==7) c++;
	int pos=1;
	while(num[pos]>9){
		num[pos]%=10;
		pos++;
		num[pos]++;
		if(num[pos]==8) c--;
		if(num[pos]==7) c++;
	}
}
int fx[]={1,1,-1,-1,1,1,-1,1,-1,1};
int b[]={0,548,866,892,64,1303,97,1278,1278,960};
int main(){

	int x;
	scanf("%d",&x);
	int k=x/100000000;
	 n=100000000*k;
	int pos=b[k],dic=fx[k];
	while(1){
		pos=(pos+dic+1336)%1337+1;
		n++;
		add();
		if(n%7==0||c>0) dic=-dic;
		if(n==x) break;
	}
	printf("%d",pos);
	return 0;
}
原文
存档评论3 条抓取于