多校第五場的題。

首先是一個好數只在某個進制下,不會是在兩個進制下都爲好數。

另外每個進制好數的個數爲d!-(d-1)!,因爲要保證第一位不爲0.

然後就是在臨界進制下有多少個好數的問題,可以變成兩個子問題,一個是找到小於n的第一個排列數,第二個是排列數的排名。

康託展開修改一下就可以變成求這兩個問題。

具體來說就是對於某個位的數字,比它小且不在前面出現的數字作爲當前位時後面無論怎麼擺都會小於這個數,so個數×剩下位數的階乘就是排名再增加的。

最後一位要+1,第一位不能爲0。區別在於如果現在的數字是前面出現過的數字,直接break掉。

比賽的時候口胡了一下,後來覺得麻煩就否決掉了23333.

其實沒有多複雜。

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define dow(i,l,r) for(int i=r;i>=l;i--)
#define rep0(i,r) for(int i=0;i<r;i++)
#define repedge(i,x)
#define maxn 50010
#define LL long long
using namespace std; const LL mm=;
int a[maxn],b[maxn],c[maxn],num[maxn],n,len,len2;
LL f[maxn];
char s[maxn];
double maxd[maxn]; void change(char *s)
{
len=strlen(s);
rep0(i,len) a[len-i-]=s[i]-'';
} void small(int *a)
{
a[]--;
rep(i,,len-)
if (a[i]<) {
a[i+]--;
a[i]+=;
}
else break;
while (len && !a[len-]) len--;
} void tobe(int m)
{
len2=;
rep0(i,len) c[i]=a[i];
int len1=len;
while (len1) {
int now=;
dow(i,,len1-) {
now=(now*+c[i]);
c[i]=now/m;
now%=m;
}
b[len2++]=now;
if (len2>m) break;
while (len1 && !c[len1-]) len1--;
}
} LL ask(int m)
{
tobe(m);
if (len2>m) return (f[m]-f[m-]+mm)%mm;
if (len2<m) return ;
rep0(i,m) num[i]=;
LL ans=-f[m-];
dow(i,,m-) {
rep0(j,b[i])
if (!num[j]) ans=(ans+f[i])%mm;
if (num[b[i]]) break;
num[b[i]]=;
if (!i) ans++;
}
return ans;
} LL calc()
{
LL sum=;
int now;
for(now=;maxd[now+]<=len-;now++);
if (now>) sum=(f[now-]-+mm)%mm;
LL more=ask(now)+ask(now+);
sum=(sum+more)%mm;
return sum;
} int main()
{
int tt;
// freopen("1009.in","r",stdin);
// freopen("1.out","w",stdout);
scanf("%d",&tt);
f[]=;
for(int i=;i<=maxn;i++) {
f[i]=f[i-]*(LL)i%mm;
maxd[i]=log(i)*(i-)/log();
}
while (tt--) {
scanf("%s",s);
change(s);
small(a);
LL ans=-calc();
scanf("%s",s);
change(s);
ans=((ans+calc())%mm+mm);
printf("%lld\n",ans%mm);
}
return ;
}

【hdu6093】Rikka with Number的更多相关文章

  1. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  2. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  3. 【Hihocoder1413】Rikka with String(后缀自动机)

    [Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...

  4. 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树

    [BZOJ4026]dC Loves Number Theory Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.    给 ...

  5. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  8. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  9. 【POJ2699】The Maximum Number of Strong Kings(网络流)

    Description A tournament can be represented by a complete graph in which each vertex denotes a playe ...

随机推荐

  1. 存一下emacs配置

    (global-set-key [f9] 'compile-file)(global-set-key [f10] 'gud-gdb)(global-set-key (kbd "C-z&quo ...

  2. 强化学习读书笔记 - 09 - on-policy预测的近似方法

    强化学习读书笔记 - 09 - on-policy预测的近似方法 参照 Reinforcement Learning: An Introduction, Richard S. Sutton and A ...

  3. 【ZABBIX】ZABBIX3.2升级3.4

    小贴士 1.停止zabbix服务 service zabbix_server stop service zabbix_agentd stop /usr/local/zabbix/sbin/zabbix ...

  4. 【Linux 运维】 date的使用

    date的使用 一.常用时间格式 #年.月.日 四位年大写,其余小写 [root@localhost ~]# date +%Y #长格式显示四位数年 [root@localhost ~]# date ...

  5. 【RL系列】从蒙特卡罗方法步入真正的强化学习

    蒙特卡罗方法给我的感觉是和Reinforcement Learning: An Introduction的第二章中Bandit问题的解法比较相似,两者皆是通过大量的实验然后估计每个状态动作的平均收益. ...

  6. python基础知识-04-字符串列表元组

    python其他知识目录 内存,cpu,硬盘,解释器 实时翻译 编译器 :一次性翻译python2,3 除法,2不能得小数,3能得小数 1.字符串操作 1.1字符串操作startswith start ...

  7. 你用 Python 做过什么有趣的数据挖掘项目?

    有网友在知乎提问:「你用 Python 做过什么有趣的数据挖掘项目?」 我最近刚开始学习 Python, numpy, scipy 等, 想做一些数据方面的项目,但是之前又没有这方面的经验.所以想知道 ...

  8. django的htpp请求之WSGIRequest

    WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.这个参数就是dja ...

  9. ASP.NET MVC5 学习系列之表单和HTML辅助方法

    一.表单 (一)Action和Method特性 Action特性用以告知浏览器信息发往何处,因此,Action特性后面需要包含一个Url地址.这里的Url地址可以是相对的,也可以是绝对的.如下Form ...

  10. ASP.NET MVC 4 内容映射

    文章:ASP.NET MVC 4 内容映射 地址:https://msdn.microsoft.com/zh-cn/library/gg416514(v=vs.108).aspx 模型-视图-控制器 ...