CodePlus7 神秘序列

在这里插入图片描述

没错,就找到这样一种方法之后二分+模拟即可。
卡时间可以去 O E I S OEIS OEIS搜一波发现 n 2 n + k ∼ π \frac {n^2}{n+k}\sim\pi n+kn2π
然后就可以 O ( n ) O(n) O(n)过掉这题了。

A C   C o d e \mathcal AC \ Code AC Code

#include<bits/stdc++.h>
#define LL long long
#define Pi 3.1415926535897932384626433832795
using namespace std;

LL K,a[3333333];
int calc(LL n){
	LL t = n + K;
	memset(a,0,(n+1)*8);
	for(int i=1;i<=n && t;i++){
		LL p = (t-1)%(i+1);	
		a[i] = i - p;
		t -= (t+i) / (i+1);
	}
	return t;
}

char cb[1<<22],*cs=cb;

template<class T>void print(T a){
	static int q[22]={};
	for(;a;a/=10) q[++q[0]] = a%10;
	if(!q[0]) putchar('0');
	for(;q[0];) putchar(q[q[0]--]+'0');
	putchar('\n');
}

int main(){

	freopen("p3.in","r",stdin);
	freopen("p3.out","w",stdout);
	scanf("%lld",&K);
	int N = round((Pi + sqrt(Pi * Pi + 4.0 * K * Pi)) / 2);
	int L = max(1,N-4) , R = N+4 , mid;
	for(;L<R;){
		int mid = (L+R)>>1;
		int t =calc(mid);
		if(t) L = mid + 1;
		else if(a[mid]){
			L = mid;
			break;
		}
		else R = mid - 1;
	}
	calc(L);
	print(L);
	for(int i=1;i<=L;i++)
		print(a[i]);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值