CF1326A Bad Ugly Numbers 题解

博客园同步发布,题目要求构造一个长度为 n 的数,条件是每个数字不为 0,且 n 不能被其各位数字整除。例如,239 合法,而 235 不合法。解决方案包括多种构造方法,如 233...33,499...99 等,这些构造都是有效的。
摘要由CSDN通过智能技术生成

博客园同步

原题链接

简要题意:

构造一个长为 n n n 的数,使得每位均不为 0 0 0,且 n n n 不被它的各位数字整除。

比方说, n = 239 n = 239 n=239 是合法的。因为:

2 ∤ 239 2 \not | 239 2239 3 ∤ 239 3 \not | 239 3239 9 ∤ 239 9 \not | 239 9239.

再比方, n = 235 n = 235 n=235 是不合法的。因为:

5 ∣ 235 5 | 235 5235.

因此,本题是个水构造。

首先 n = 1 n = 1 n=1,显然无解。

否则,考虑以下构造:

233 ⋯ 33 233 \cdots 33 23333

499 ⋯ 99 499 \cdots 99 49999

277 ⋯ 77 277 \cdots 77 27777

577 ⋯ 77 577 \cdots 77 57777

599 ⋯ 99 599 \cdots 99 59999

899 ⋯ 99 899 \cdots 99 89999

677 ⋯ 77 677 \cdots 77 67777

⋯ \cdots

(盲猜不下 1000 1000 1000 种构造,全部合法)

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int main(){
	int T=read(); while(T--) {
		int n=read();
		if(n==1) printf("-1\n");
		else {
			putchar('4');
			for(int i=1;i<n;i++) putchar('9');
			putchar('\n');
		} //499...99
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值