首页 > 其他分享 >AtCoder Regular Contest 129 C Multiple of 7

AtCoder Regular Contest 129 C Multiple of 7

时间:2023-05-12 22:35:18浏览次数:43  
标签:AtCoder typedef Multiple Contest int long Regular define

洛谷传送门

AtCoder 传送门

首先 \(\text{7777...777}\)(\(x\) 个 \(7\))对能被 \(7\) 整除子串数量的贡献是 \(\frac{x(x+1)}{2}\)。

把 \(n\) 分解成若干 \(x_i\) 使得 \(\sum\limits_{i=1}^m \frac{x_i(x_i+1)}{2} = n\),表示每段 \(x_i\) 个 \(7\)。怎么把它们组合在一起呢?

一个 naive 的想法是 \(\text{77...77a77...77a}\),其中 \(a\) 为指定的数。发现怎么指定,中间都会串出一段 \(7\) 的倍数。

既然不能指定,那么就随机好了!随机得出每段 \(7\) 之间间隔的数,直到合法就输出,可过。

code
// Problem: C - Multiple of 7
// Contest: AtCoder - AtCoder Regular Contest 129
// URL: https://atcoder.jp/contests/arc129/tasks/arc129_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 1000100;

int n, m;
char s[maxn];

void solve() {
	scanf("%d", &n);
	int t = n;
	vector<int> vc;
	while (n) {
		int l = 1, r = 1e4, pos = -1;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (mid * (mid + 1) / 2 <= n) {
				pos = mid;
				l = mid + 1;
			} else {
				r = mid - 1;
			}
		}
		vc.pb(pos);
		n -= pos * (pos + 1) / 2;
	}
	mt19937 rnd(time(NULL));
	while (1) {
		m = 0;
		for (int x : vc) {
			for (int _ = 0; _ < x; ++_) {
				s[++m] = '7';
			}
			s[++m] = '1' + rnd() % 9;
		}
		int cnt = 0;
		for (int i = 1; i <= m; ++i) {
			int x = 0;
			for (int j = i; j <= m; ++j) {
				x = (x * 10 + s[j] - '0') % 7;
				cnt += (!x);
			}
		}
		if (cnt == t) {
			break;
		}
	}
	printf("%s\n", s + 1);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:AtCoder,typedef,Multiple,Contest,int,long,Regular,define
From: https://www.cnblogs.com/zltzlt-blog/p/17396442.html

相关文章

  • AtCoder Beginner Contest 163 F path pass i
    洛谷传送门AtCoder传送门感觉我的做法比较奇葩(容斥,总路径数减去只走点权为\(k\)的路径。设点权为\(k\)的点数为\(c_k\),点权不为\(k\)的点构成的每个连通块大小为\(s_i\),那么\(ans_k=\frac{n(n-1)}{2}-\sum\frac{s_i(s_i-1)}{2}+c_k\)。考虑快速计算\(\su......
  • AtCoder Beginner Contest 207 F Tree Patrolling
    洛谷传送门AtCoder传送门简单树形dp。设\(f_{u,i,p=0/1,q=0/1}\)为\(u\)的子树中被覆盖点数为\(i\),\(u\)有没有被覆盖,\(u\)有没有被选。转移树形背包合并即可,需要分类讨论。要注意如果\(u\)没被覆盖,\(v\)选了,或者\(u\)选了,\(v\)没被覆盖,被覆盖点数要\(+1\)。......
  • AtCoder Beginner Contest 177 F I hate Shortest Path Problem
    洛谷传送门AtCoder传送门设\(f_{i,j}\)为从第\(1\)行到\((i+1,j)\)的最短路。因为我们并不关心最后到达的是哪一个格子,于是强制\(f_{i,j}\)为必须从\((i,j)\)往下走一格到\((i+1,j)\)的最短路。有转移:\[f_{i,r+1}\getsf_{i-1,j}+r+2-j,j\in[l......
  • AtCoder Beginner Contest 152 A-E
    AtCoderBeginnerContest152A-ACorWAvoidsolve(){intn=read(),m=read();puts(n==m?"Yes":"No");}B-ComparingStringsvoidsolve(){intn=read(),m=read();if(m<n)swap(n,m);for(inti=1;i<=m;i++){......
  • AtCoder 好题选做
    AtCoderRegularContest091-F-StrangeNimhttps://atcoder.jp/contests/arc091/tasks/arc091_d清北学堂讲的一道题,我艹感觉这结论很难猜啊。做这种题一定是先写爆搜打表啊,先写了一个博弈论求SG函数:然后发现了一个规律:\(\text{SG}(nk,k)=n\)。还有一个规律:当\(n<k......
  • AtCoder Beginner Contest 298 题解
    题面:https://atcoder.jp/contests/abc298/tasks_printA-JobInterview直接模拟即可。#include<bits/stdc++.h>#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/tree_policy.hpp>#include<ext/pb_ds/hash_policy.hpp>usingnamespace......
  • SLF4J: Class path contains multiple SLF4J bindings.错误解决
    1.出现问题错误如下:SLF4J:ClasspathcontainsmultipleSLF4Jbindings.SLF4J:Foundbindingin[jar:file:/D:/Users/FFprincess/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J:Foun......
  • AtCoder DP系列刷题记录
    直面自己的弱点。AFrog1简单线性\(\text{dp}\),令\(dp_i\)为跳到第\(i\)块石头的最小花费,易得:\[dp_i+=\min(|a_i-a_{i-1}|,|a_i-a_{i-2}|)\]BFrog2很快就写完了,但是一直调了十分钟,耻辱啊。如果反着跳,第\(i\)根木桩只能从第\(i+1\)或\(i+2\)木桩上跳到,则有:\[d......
  • AtCoder Beginner Contest 234 Ex Enumerate Pairs
    洛谷传送门AtCoder传送门把每个点分到\((\left\lfloor\frac{x}{K}\right\rfloor,\left\lfloor\frac{y}{K}\right\rfloor)\)的正方形内,枚举相邻正方形,计入答案。正确性显然。复杂度证明就是所有每个正方形内距离为\(K\)的点对下界为\(\Omega(n^2)\)。考虑分成四个边长为......
  • AtCoder Beginner Contest 221 F Diameter set
    洛谷传送门AtCoder传送门显然,选出的每两个点都要组成一条直径。还是显然,每条直径的重合部分只可能是一个点或一条边。简单证一下,没有重合显然不可能,重合超过一个点或一条边,直径会更长。进一步发现,设直径点数为\(x\),如果\(x\nmid2\),重合部分是一个点,否则重合部分是一条边......