ARC129C

Sol

不难想到可以答案可以为如下形式:777...777a777...77b....777...777,其中字母表示数字。注意到段数不会太多,那么直接包搜间隔的数字即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define pf push_front
#define desktop "C:\\Users\\incra\\Desktop\\"
#define IOS ios :: sync_with_stdio (false),cin.tie (0),cout.tie (0)
#define debug(x) cerr << #x << ' ' << x << endl
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
template <typename T1,typename T2> bool tomax (T1 &x,T2 y) {
	if (y > x) return x = y,true;
	return false;
}
template <typename T1,typename T2> bool tomin (T1 &x,T2 y) {
	if (y < x) return x = y,true;
	return false;
}
LL power (LL a,LL b,LL p) {
	LL ans = 1;
	while (b) {
		if (b & 1) ans = ans * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return ans;
}
int fastio = (IOS,0);
#define endl '\n'
#define puts(s) cout << (s) << endl
int n;
vector <string> v;
bool check (string s) {
	int cnt = 0;
	for (int i = 0;i < s.size ();i++) {
		int sum = 0;
		for (int j = i;j < s.size ();j++) {
			sum = (sum * 10 + s[j] - '0') % 7;
			cnt += !sum;
		}
	}
	return cnt == n;
}
void DFS (int u,string ans) {
	if (u == v.size () - 1) {
		ans += v.back ();
		if (check (ans)) {
			cout << ans << endl;
			exit (0);
		}
		return ;
	}
	for (int i = 1;i <= 9;i++) DFS (u + 1,ans + v[u] + (char)('0' + i));
}
void mian () {
	cin >> n;
	int x = n;
	for (int i = 2e3;i >= 1;i--) {
		string tmp;
		for (int j = 1;j <= i;j++) tmp += '7';
		while (i * (i + 1) / 2 <= x) {
			x -= i * (i + 1) / 2;
			v.pb (tmp);
		}
	}
	DFS (0,"");
	puts ("-1");
}
int main () {
	int T = 1;
	// cin >> T;
	while (T--) mian ();
	return 0;
}
posted @ 2025-03-16 17:18  incra  阅读(11)  评论(0)    收藏  举报