首页 > 其他分享 >Atcoder ARC060D Digit Sum

Atcoder ARC060D Digit Sum

时间:2023-07-26 20:44:19浏览次数:46  
标签:Atcoder Digit le ll sqrt ARC060D https Sum

看到 \(n\le 10^{11}\),考虑按根号分为两部分处理。

对于 \(b\le \sqrt{n}\),考虑直接暴力算 \(\operatorname{f}(b, n)\) 判断是否等于 \(s\),这部分的计算量是 \(O(\sqrt{n})\) 级别的。

对于 \(\sqrt{n} < b \le n\),则这个时候在 \(b\) 进制下 \(n\) 也只有两位,考虑列出 \(n, s\) 与 \(n\) 在 \(b\) 进制下的数 \(\overline{xy}\) 的关系:
\(\begin{cases}n = bx + y\\ s = x + y\end{cases}\)
于是就有 \(n - s = (b - 1)\times x\),考虑枚举 \(n - s\) 的因数得到 \(b - 1\) 再去检验 \(\operatorname{f}(b, n)\) 的值是否为 \(s\),容易得到这部分的时间复杂度也是 \(O(\sqrt{n})\) 的。

除此之外还有个情况 \(b > n\),这个时候 \(\operatorname{f}(b, n) = n\),判掉就行。

综上,时间复杂度为 \(O(\sqrt{n})\)。

// lhzawa(https://www.cnblogs.com/lhzawa/)
// Problem: D - Digit Sum
// Contest: AtCoder - AtCoder Regular Contest 060
// URL: https://atcoder.jp/contests/arc060/tasks/arc060_b
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using ll = long long;
ll f(ll n, ll b) {
	return n % b + (n >= b ? f(n / b, b) : 0);
}
int main() {
	ll n, s;
	scanf("%lld%lld", &n, &s);
	if (n == s) {
		printf("%lld\n", n + 1);
		return 0;
	}
	ll w = 2;
	for (; w * w <= n; w++) {
		if (f(n, w) == s) {
			printf("%lld\n", w);
			return 0;
		}
	}
	ll ans = -1;
	for (ll i = 1; i * i <= n - s; i++) {
		if ((n - s) % i == 0 && f(n, (n - s) / i + 1) == s) {
			ans = (n - s) / i + 1;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

标签:Atcoder,Digit,le,ll,sqrt,ARC060D,https,Sum
From: https://www.cnblogs.com/lhzawa/p/17583506.html

相关文章

  • uva 10061 How many zero's and how many digits ?(在不同进制下分解因子)
                             uva10061Howmanyzero'sandhowmanydigits?Givenadecimalintegernumberyouwillhavetofindouthowmanytrailingzeroswillbethereinitsfactorialinagivennumbersystemandalsoyouwillhaveto......
  • AtCoder Beginner Contest 311
    AtCoderBeginnerContest311FirstABC思路:找到第一个a,b,c都出现的位置#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string&......
  • AtCoder Beginner Contest 311
    A-FirstABC#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongint32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);intn;strings;cin>>n>>s;set<char>c......
  • Vue项目启动 报错error:0308010C:digital envelope routines::unsupported
    出现这个错误是因为node.jsV17版本中最近发布的OpenSSL3.0,而OpenSSL3.0对允许算法和密钥大小增加了严格的限制,可能会对生态系统造成一些影响.解决方法package.json增加配置"scripts":{"serve":"setNODE_OPTIONS=--openssl-legacy-provider&&vue-cli-serviceserve......
  • Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311)
    ToyotaProgrammingContest2023#4(AtCoderBeginnerContest311)A-FirstABC(atcoder.jp)记录一下\(ABC\)是否都出现过了然后输出下标#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;signedmain(){ios::sync_with_stdio(false);cin.tie(n......
  • Atcoder ARC058E Iroha and Haiku
    题目中的式子转化一下即存在一位\(i\)使得到\(i\)时的后缀和存在\(X+Y+Z,Y+Z,Z\),再发现\(X+Y+Z\le17\),考虑状压。设\(f_{i,j}\)为填了\(i\)个数当前后缀和中存在的数的状态为\(j\)(只存\(0\simX+Y+Z\)的数是否存在)的方案数。考虑转移,则下一位可......
  • 「解题报告」Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311)
    比赛地址:ToyotaProgrammingContest2023#4(AtCoderBeginnerContest311)-AtCoder后记:大家都太强了%%%,如果我做不出第四题就要掉分了。。。A-FirstABCA-FirstABC(atcoder.jp)找到第一个\(\texttt{A,B,C}\)三种字符都出现的位置。/*Thecodewaswrittenby......
  • Atcoder ARC058B Iroha and a Grid
    考虑从第\(b\)列与第\(b+1\)之间分开这个矩阵,钦定\((i,b)\)下一步必须走到\((i,b+1)\),可以发现这样是不会漏算或算重的。于是就可以用乘法原理算出这个\(i\)的贡献:\(\binom{(i-1)+(b-1)}{i-1}\times\binom{(n-i)+(m-b-1)}{n-i}\),左半部分的就......
  • 练习记录-AtCoder Beginner Contest 311-(A-E)
    写的还挺顺的F之后补A-FirstABC找abc三个字母什么时候出现了一次输出即可B-VacationTogether题意:最长的几个人一排里面均有时间#include<bits/stdc++.h>#defineclosestd::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)usingnamespacestd;typedeflon......
  • Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311)——D
    https://atcoder.jp/contests/abc311/tasks/abc311_d思路题目说如果当前方向的下一个点能走,那么就一直走,否则才能转向。根据题意模拟即可,这道题的难点在于,碰到已经走过的点到底要不要走。如果当前方向走到底,其中的点之前全部都走过那么就不能再走了。我们用bfs模拟,对于能一直走......