多校第五場的題。

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

另外每個進制好數的個數爲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. Elasticsearch5.x版本中对Text类型进行聚合时提示illegal_argument_exception

    Having this field in my mapping "answer": { "type": "text", "fiel ...

  2. EF6+MVC5之Oracleo数据库的CodeFirst方式实现

    http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.htm http://www.knowsky.com/887470.htm ...

  3. 使用PHP写ajax接口

    使用PHP写ajax接口 之前有学过php都是前后端没有分离的,所以也想去了解后端是怎么写出ajax接口的,可能问了别人或者上网找了很多资料都很有有点懵,或者说直接用TP或者lavarel这些后端框架 ...

  4. linux源码学习-for_each_cpu

    刚开始读linux源码,第一眼就看到了这个很有意思的函数族,周末好好研究一下 3.13 这个组都是宏定义for循环,分析的时候注意到cpumask_next(),它在一个文件中定义了两次,还不是重载, ...

  5. linux下搭建python机器学习环境

    前言 在 linux 下搭建 python 机器学习环境还是比较容易的,考虑到包依赖的问题,最好建立一个虚拟环境作为机器学习工作环境,在建立的虚拟环境中,再安装各种需要的包,主要有以下6个(这是看这个 ...

  6. Amazon Headlines Update on Activity in US West Coast Ports

    According to news reports, freighter cargo may not be offloaded at U.S. West Coast ports from Februa ...

  7. Amazon及其亏本诱饵策略还能坚持多久?

    Amazon 刚刚公布了最新的财报,亏损 4100 万美元.这是这家电子商务巨头连续 5 个季度以来的第 3 次亏损.但是华尔街似乎却一片叫好声,当日 Amazon 的股价也涨了近 8 个点达 359 ...

  8. component-scan标签的use-default-filters属性的作用以及原理分析

    一.背景 ​ 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,有时候就是因为包扫描出了问题,其中component-scan的标签的use-def ...

  9. Knight Moves(广搜BFS)

    Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to ...

  10. 欢迎来怼--第三十次Scrum会议

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/18 17:20~17:53,总计33min. 地 ...