Problem Description
If 2001! were written in base 23,how many trailing zeros would there be?
Problem Report
刚起床就看到Jean留言说有道题 ˊ_>ˋ,真是提神醒脑^—^
乍一看像一道Number Theory,其实只需要求末尾零,突破口是模拟进制转换的过程。我们在求十进制转n进制的过程中是先用n(这里是23)对原数x(这里是2001!)进行求模(mod),取每次得到的余数。下面例举一个进制转换过程:
假设我们要将25921转换为23进制
- 25921 div 23=1127……0
- 1127 div 23=49……0
- 49 div 23=2……3
- 2 div 23=0……2
最后得到结果就是2300
我们可以观察到,25921实际上是可以分解为23*23*7*7的其中因子23的个数就决定了23进制数的尾零个数。因此我们的问题就转化为了求2001!能分解出多少个23,因为23是质数,所以我们不妨采用反向来构造来解决它:
23*1 在 1 to 2001范围内
23*2 在 1 to 2001范围内
23*3 在 1 to 2001范围内
……
23*23 在 1 to 2001范围内
……
23*46 在 1 to 2001范围内
……
23*69 在 1 to 2001范围内
……
23 * 87 在 1 to 2001 范围内
23 * 88 不在 1 to 2001 范围内
因此我们看到在1 to 2001 范围内,总共有87个数可以被23整除,其中特别需要注意的是23*23,23*46,23*69这三个数,它们分别可以被两个23整除,所以我们可以得到最后的答案为87+3=90。
My Source Code
// Created by Chlerry in 2015.
// Copyright (c) 2015 Chlerry. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array, x) memset(array,x,sizeof(array))
typedef pair<int,int> P;
int main()
{
//freopen("in.txt","r",stdin);
int ans=0;
for(int i=1;i<=2001;i++)
{
int t=i;
while(t%23==0)
t/=23,ans++;
}
cout<<ans<<endl;
return 0;
}