Editorial for Số lượng số
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của ladpro98
#include <cstring> #include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <climits> #include <cstdlib> #include <ctime> #include <memory.h> #include <cassert> #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define REP(i, a, b) for(int i = (a); i <=(b); i++) #define FORD(i, a, b) for(int i = (a); i > (b); i--) #define REPD(i, a, b) for(int i = (a); i >=(b); i--) #define TR(it, a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); ++it) #define SZ(a) (int((a).size())) #define ALL(a) (a).begin(), (a).end() #define PB push_back #define MP make_pair #define LL long long #define LD long double #define II pair<int, int> #define X first #define Y second #define VI vector<int> const int N = 20; using namespace std; typedef unsigned LL i64; i64 POW[N], F[N][N * 9], a, b, A, B; bool was[N][N * 9]; int ntest; i64 DP(int i, i64 n, int sum) { i64 l = n * POW[i], r = (n + 1) * POW[i] - 1; if (r < A || B < l) return 0; if (i == 0) return n && sum == 0; if (A <= l && r <= B && was[i][sum]) return F[i][sum]; i64 ans = 0; REP(j, 0, 9) if (sum >= j) ans += DP(i - 1, n * 10 + j, sum - j); if (A <= l && r <= B) was[i][sum] = 1, F[i][sum] = ans; return ans; } int main() { POW[0] = 1; FOR(i, 1, N) POW[i] = POW[i - 1] * 10; cin >> ntest; while (ntest--) { cin >> a >> b; i64 res = 0; FOR(sum, 1, 172) { A = (a % sum == 0) ? (a / sum) : (a / sum + 1); B = b / sum; res += DP(19, 0, sum); } cout << res << '\n'; } return 0; }
Code mẫu của RR
#include <iostream> #include <algorithm> #include <cstring> #define FOR(i,a,b) for(int i=(a), _b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=a; i>=b; i--) #define REP(i,a) for(int i=0; i<a; i++) #define ll long long using namespace std; int a[20], now; ll f[20][200][2]; int memo[20][200][2]; void init(ll gh) { ll save = gh; memset(a, 0, sizeof a); if (!gh) a[0] = 1; while (gh) { a[0]++; gh /= 10; } gh = save; int now = a[0]; while (gh) { a[now--] = gh%10; gh /= 10; } } ll dp(int i, int sum, bool lower) { if (memo[i][sum][lower] == now) return f[i][sum][lower]; memo[i][sum][lower] = now; if (i == a[0]) { if (sum > 9 || (!lower && sum > a[i])) return f[i][sum][lower] = 0LL; else return f[i][sum][lower] = 1LL; } f[i][sum][lower] = 0LL; FOR(now, 0, min(lower?9:a[i],sum)) { f[i][sum][lower] += dp(i+1, sum - now, lower || (now < a[i])); } return f[i][sum][lower]; } ll get(ll gh) { ll res = 0; FOR(sum, 1, min(gh,145LL)) { now ++; init(gh / sum); res += dp(1, sum, false); } return res; } int main() { int test; scanf("%d", &test); while (test--) { ll a, b; scanf("%lld %lld", &a, &b); printf("%lld\n", get(b) - get(a-1)); } return 0; }
Code mẫu của hieult
#pragma comment(linker, "/STACK:16777216") #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<cassert> #include<ctime> #include<algorithm> #include<iterator> #include<iostream> #include<cctype> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<list> //#include<conio.h> #define ep 0.000000001 #define maxn 10011 #define oo 1111111111 #define modunlo 111539786 #define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++) #define ull unsigned long long double const PI=4*atan(1); using namespace std; typedef pair<int, int> II; typedef vector<int> VI; typedef vector<II> VII; typedef vector<VI> VVI; typedef vector<VII> VVII; int test; ull a,b,f[20][200] = {0}, g[20][200] = {0}; ull tinh(int r, ull x){ if( r == 0) return 1; if( x == 0) return 0; int n = 0; ull y = 0, res = 0, T = 1; while(true){ if( x < 10){ for(int j = 0; j < x; j++) if( j <= r) res += g[n][r-j]; break; } else{ y += (x % 10) * T; x /= 10; T *= 10; n++; } } if(x <= r) res += tinh(r - x, y); return res; } ull tim(ull x){ if(x == 0) return 0; ull res = 0; for(int i = 1; i < 200; i++){ res += tinh(i, x/i); } return res; } int main(){ //freopen("input.in","r",stdin); //freopen("output.out","w",stdout); f[0][0] = 1; g[0][0] = 1; for(int i = 0; i < 10; i++) f[1][i] = 1, g[1][i] = 1; for(int i = 2; i <= 19; i++){ for(int j = 1; j < 10; j++){ for(int k = j; k < 200; k++){ f[i][k] += g[i-1][k-j]; } } for(int j = 0; j < 200; j++) g[i][j] = g[i-1][j] + f[i][j]; } //cout<<tim(10000); scanf("%d",&test); //test = 0; for(int itest = 0; itest < test; itest++){ cin >> a >> b; // cout<< a <<" "<<b<<endl; cout << tim(b) - tim(a - 1) << endl; } }
Code mẫu của khuc_tuan
#include <iostream> #include <cstdio> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <cstring> #include <cmath> using namespace std; #define Rep(i,n) for(int i=0;i<(n);++i) #define For(i,a,b) for(int i=(a);i<=(b);++i) #define Ford(i,a,b) for(int i=(a);i>=(b);--i) #define fi first #define se second #define pb push_back #define MP make_pair typedef pair<int,int> PII; typedef vector<int> VI; int a[22],na; long long T[22][200]; long long calc(long long X,int s){ for(na=0;X>0;a[na++]=X%10,X/=10); if(na==0)return 0; int total=0,cur=0; Rep(i,na)total+=a[i]; long long res=total==s; Ford(i,na-1,0) { Rep(j,a[i])if(s>=j+cur)res+=T[i][s-j-cur]; cur+=a[i]; } return res; } int main() { T[0][0]=1; For(i,1,20)For(j,0,199){ Rep(ad,10)if(ad<=j)T[i][j]+=T[i-1][j-ad]; } int st; cin>>st; Rep(t,st){ long long A,B,u,v,total=0; cin>>A>>B; For(s,1,162){ u=A/s; if(u*s<A)++u; v=B/s; if(u<=v)total+=calc(v,s)-calc(u-1,s); } cout<<total<<endl; } return 0; }
Comments