[violet2]sillyz

题意:定义S(n) = n*各数位之积,然后给定L<=R<=10^18,求有多少个n在[L,R]区间内

思路:

看了半天无从下手。。看完题解才豁然开朗。。

具体思路看vani神博客吧。讲的很清楚。。

code:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
const ll Inf = 1000000000LL;
int d[maxn], dc[maxn][], tot;
int td, tc[]; void dfs(int r, int s, ll num){
if (num > Inf) return;
if (r == || s == ){
d[tot] = num;
for (int i = ; i < ; ++i)
dc[tot][i] = tc[i];
tot++;
return;
}
dfs(r + , s, num);
tc[r-]++;
dfs(r, s + , num * r);
tc[r-]--;
} ll frac[];
ll count(int m){
int left = m;
ll res = frac[m];
for (int i = ; i < ; ++i) left -= tc[i], res /= frac[tc[i]];
res /= frac[left];
return res;
} int bit[]; ll calculate(ll b, int p){
if (b <= ) return ;
int k = ;
while (b > ) bit[++k] = b % , b /= ;
int size = ;
for (int i = ; i < ; ++i)
tc[i] = dc[p][i], size += tc[i];
ll res = ;
for (int i = ; i < k; ++i)
if (i >= size) res += count(i);
for ( ;k > ; --k){
if (bit[k] == || size > k) break;
for (int i = ; i < bit[k]; ++i) if (tc[i-]){
--tc[i-], --size;
if (k > size) res += count(k - );
++tc[i-], ++size;
}
if (bit[k] >= ){
if (k > size) res += count(k-);
if (tc[bit[k]-] == ) break;
--tc[bit[k]-], --size;
}
}
return res;
} ll calculate(ll x){
if (x <= ) return ;
ll res = ;
for (int i = ; i < tot; ++i)
res += calculate(x / d[i] + , i);
return res;
} void pre_do(){
frac[] = ;
for (int i = ; i <= ; ++i) frac[i] = frac[i-] * i;
memset(tc, , sizeof(tc));
tot = ;
dfs(, , );
} int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
pre_do();
ll l, r;
while (cin >> l >> r){
ll ans = calculate(r) - calculate(l - );
cout << ans << endl;
}
return ;
}
上一篇:thinkphp5实现定位功能


下一篇:自用广告过滤规则,整合xwhyc大大的,非常小才79K