codeforces1373E Sum of Digits

https://codeforces.com/problemset/problem/1373/E

其實這題挺水的,然而看見F好像更可做一些,就去WA F一直到結束了。。。

看了jly的代碼。。清晰易懂思路簡單。。。

這題關鍵的問題在於那種末尾好多個9,然後進位,就一片0,於是並不知道這樣如何構造

其實就可以直接枚舉末尾有多少個9就行了,枚舉完以後,我們算出這d個9和末尾上的數字c,在k+1個數字中會產生多少貢獻,然後再看剩下還要湊多少值,如果<=8,那麼就是這個數+d個9+末尾c就行了,如果>8,那麼就是a99989999c這樣的結構能最小。

枚舉一個c和d個9,然後構造出最小值,更新記錄答案就行了。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;

int n,m,cas,k,cnt,tot;
int a[maxl],b[maxl];
char s[maxl];
bool in[maxl]; 
ll ans;

inline void prework()
{
	scanf("%d%d",&n,&k);
} 

inline void mainwork()
{
	ans=-1;int tmp,res;__int128 num;
	for(int c=0;c<=9;c++)
		for(int d=0;9*d+c<=n;d++)
		{
			tmp=0;
			for(int i=0;i<=k;i++)
			if(i+c>=10)
				tmp+=1+(i+c)%10;
			else
				tmp+=9*d+i+c;
			res=n-tmp;
			num=0;
			for(int i=1;i<=d;i++)
				num=num*10+9;
			num=num*10+c;
			if(res<0 || res%(k+1)!=0)
				continue;
			if(res==0)
			{
				if(ans==-1 || num<ans)
					ans=num;
				continue;
			}
			num=0;res=res/(k+1);
			if(res<=8)
			{
				num=res;
				for(int i=1;i<=d;i++)
					num=num*10+9;
				num=num*10+c;
			}
			else
			{
				res-=8;
				num=res%9;
				res-=num;
				for(int i=1;i<=res/9;i++)
					num=num*10+9;
				num=num*10+8;
				for(int i=1;i<=d;i++)
					num=num*10+9;
				num=num*10+c;
			}
			if(num<1e18 && (ans==-1 || num<ans))
				ans=num;
		}
}

inline void print()
{
	printf("%lld\n",ans);
}

int main()
{
	int t=1;
	scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章