Factorial Problem in Base K

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3621

Description

How many zeros are there in the end of s! if both s and s! are written in base k which is not necessarily to be 10? For general base, the digit order is 0-9,A-Z,a-z(increasingly), for example F4 in base 46 is actually 694 in base 10,and f4 in base 46 is 1890 in base 10.

Input

There are multiple cases(less than 10000). Each case is a line containing two integers s and k(0 ≤ s < 2^63, 2 ≤ k ≤ 62).

Output

For each case, output a single line containing exactly one integer in base 10 indicating the number of zeros in the end of s!.

Sample Input

101 2
12 7
 

Sample Output

31

HINT

题意

给你个在k进制下的数S,然后求S!在K进制下,有多少个末尾0

题解:

首先在10进制下,我们是怎么做的?我们先对10进行了质因数分解,分解成了2和5,然后我们就统计s!中,2和5各有多少个,然后取最少的就好了

就这样,我们先对k进行质因数分解,然后我们取最少个数就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline void P(int x)
{Num=0;if(!x){putchar('0');puts("");return;}while(x>0)CH[++Num]=x%10,x/=10;while(Num)putchar(CH[Num--]+48);puts("");
}
//**************************************************************************************string s;
int n;
const int p[18]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61};
int a[30];
int main()
{while(cin>>s>>n){memset(a,0,sizeof(a));ll tmp=0;ll k=1;for(int i=s.size()-1;i>=0;i--){if(s[i]<='9'&&s[i]>='0')tmp+=(s[i]-'0')*k;else if(s[i]<='Z'&&s[i]>='A')tmp+=(s[i]-'A'+10)*k;else tmp+=(s[i]-'a'+36)*k;k*=n;}for(int i=0;i<18;i++){while(n%p[i]==0&&n>0){n/=p[i];a[i]++;}}ll ans=(1LL<<63)-1;for(int i=0;i<18;i++){ll now=tmp,tot=0;while(now>0){now/=p[i];tot+=now;}if(a[i]>0)ans=min(ans,tot/a[i]);}printf("%lld\n",ans);}}

转载于:https://www.cnblogs.com/qscqesze/p/4539970.html

zoj 3621 Factorial Problem in Base K 数论 s!后的0个数相关推荐

  1. zoj 4745 Factorial Problem in Base K

    题目链接:点击打开链接 题目的意思就是:给一个k进制的数s,求s!在10 进制下的末尾0个数. 思路: 先把s转化为10进制下的数. 把n!分解质因数. 把k分解质因数. 求所有的k的质因数中,除以n ...

  2. ZOJ - 4049 Halting Problem 题解【c++】

    题目如下: In computability theory, the halting problem is the problem of determining, from a description ...

  3. 漫画:删去k个数字后的最小值

    转载自  漫画:删去k个数字后的最小值 我们来举一个栗子: 给定整数 541270936,要求删去一个数,让剩下的整数尽可能小. 此时,无论删除哪一个数字,最后的结果都是从9位整数变成8位整数.既然同 ...

  4. LeetCode 6039. K 次增加后的最大乘积(优先队列)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个非负整数数组 nums 和一个整数 k .每次操作,你可以选择 nums 中 任一 元素并将它 增加 1 . 请你返回 至多 k 次操作后,能得到 ...

  5. 给定n位正整数a,去掉其中任意k个数字后,剩下的数字按原次序排列组成⼀个新的正整数,求组成的新数最小的删数方案(O((n-k)logk)优化)

    问题描述 给定n位正整数a,去掉其中任意k个数字后,剩下的数字按原次序排列组成⼀个新的正整数.对于给定的n和k,设计⼀个算法,找出剩下数字组成的新数最少的删数方案. 这一道题来自zyq老师的算法分析与 ...

  6. 计算二叉树的第k层中所有叶子结点个数

    计算二叉树的第k层中所有叶子结点个数 Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 二叉链表表示的二叉树:按先序次序输入二叉树中结点 ...

  7. 【2202. K次操作后最大化顶端元素】贪心算法

    注意分类讨论以及特殊情况! class Solution { public:int maximumTop(vector<int>& nums, int k) {int len=nu ...

  8. P2522 HAOI2011 Problem b [莫比乌斯反演,数论分块]

    P2522 HAOI2011 题意 对于给出的n个询问,每次求有多少个数对(x,y)(x,y)(x,y),满足a≤x≤ba≤x≤ba≤x≤b,c≤y≤dc≤y≤dc≤y≤d,且gcd(x,y)=kgc ...

  9. Problem B: 第K位数字

    Problem Description 编写函数digit(n, k),使得函数返回正整数 n 中的第 k 位数字(从右边算起).如果 k 大于 n 所含有的数字个数,函数返回 0 .  测试程序为: ...

最新文章

  1. 论述Linux文件系统,linux文件系统概述
  2. Zookeeper系列五:Master选举、ZK高级特性:基本模型
  3. java 令牌解析_Java编程guava RateLimiter实例解析
  4. 使用 Boost.Math 计算 Jacobi Zeta 函数的简单示例, 并使用相应的 WolframAlpha 命令
  5. ABAP web service schema node的处理
  6. epublib java_使用Epublib处理epub文件 | 学步园
  7. 文件共享服务器imac,iMac怎么在网络上共享设备windows文件夹和服务 | MOS86
  8. win10远程计算机或设备将不接收连接?
  9. 【react】 redux 公共状态管理---数据的渲染,数据的修改,再把修改的数据渲染到当前组件...
  10. 分析Redis集群原理
  11. 1. 架构到底是指什么? 容易混淆的概念
  12. 【计组】计算机乘法运算
  13. ALFA | 临床基因组课程 人群频率数据库 新成员
  14. 数据库设计阶段 各阶段的任务
  15. bios不识别 光驱固态_bios识别不到固态硬盘
  16. Java使用Thylemeaf + iText实现html(带图片)转pdf文件
  17. VC++6.0如何找到“丢失”的对话框控件工具箱
  18. Ubuntu18.04图标显示实时网速
  19. CSP 201409-5 拼图问题(给出一个n×m的方格图,现在要用如下L型的积木拼到这个图中......)
  20. Tiled结合Unity实现瓦片地图

热门文章

  1. datagrip防止xx后弹窗
  2. linux下的bc计算器设置scale精度
  3. xfce开始菜单增加一个新的图标
  4. ubuntu16.04/20.04 xfce4下面使用护眼软件redshift
  5. python核心数据类型_Python核心数据类型——字符串
  6. 方法的反射---反射学习笔记(二)
  7. Unlock SAP Data
  8. 181. maven项目ssm(父工程 子工程)
  9. goroutine, channel 和 CSP
  10. mysql二进制安装