Topcoder SRM 569 1000pt
来源:互联网 发布:淘宝美工兼职 编辑:程序博客网 时间:2024/05/01 11:37
Problem Statement
The factorial of the k-th order of a number n is denoted n!k and defined by the following recurrences:
1) n!k = n!(k-1) * (n-1)!k for n > 0 and k > 0
2) n!k = 1 for n = 0
3) n!k = n for k = 0For example, 7!1 = 7! (the traditional factorial), and 5!3 = 5!2 * 4!3 = (5!1 * 4!2) * 4!3.
You are given s N, K and B. Count the number of trailing zeros in the number N!K when it is written in base B and return it modulo 1,000,000,009.
Definition
Class: MegaFactorial
Method: countTrailingZeros
Parameters: int, int, int
Returns: int
Method signature: int countTrailingZeros(int N, int K, int B)
(be sure your method is public)Limits
Time limit (s): 840.000
Memory limit (MB): 64
Constraints
- N will be between 1 and 1,000,000,000 (10^9), inclusive.
- K will be between 1 and 16, inclusive.
- B will be between 2 and 10, inclusive.Examples
0) 6 1 4
Returns: 2
6!1 = 6! = 23100 in base 4.
1) 4 2 6
Returns: 2
4!2 = 4!1 * 3!2 = … = 4! * 3! * 2! = 1200 in base 6.
2) 10 3 10
Returns: 22
3) 50 10 8
Returns: 806813906
4) 1000000000 16 2
Returns: 633700413This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
我A的Topcoder第一道题啊,纪念一下。
题目非常简洁啊(似乎某人说过“题目越简洁,就越是暗藏杀机”),题目中定义了一种函数
解法一:
不妨单独考虑第一行中
解法二:
我们来考虑暴力一点的做法,观察到
的形式,然后矩阵
但是注意还没有结束!我们得到了
(TC为了时间效率,
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;class MegaFactorial{ typedef long long ll; int n,m,K,base,aa[50],BB,top;ll mod; struct data{ int x,y,a[20][20]; data(){x=y=0;memset(a,0,sizeof(a));} void data1(){x=y=18;memset(a,0,sizeof(a));for (int i=1;i<=18;++i)a[i][i]=1;} }A[50],B[50],ans; public:int cheng(int a,int b) { ll x=a,y=b,z=(x*y)%mod; return (int)z; } public:ll power(ll a,ll b) { ll ans=1; for (;b;b>>=1,a=(a*a)%mod) if(b&1)ans=(ans*a)%mod; return ans; } public:data ch(data a,data b) { data c;c.x=a.x,c.y=b.y; for (int i=1;i<=a.x;++i) for (int j=1;j<=b.y;++j) for (int k=1;k<=a.y;++k) c.a[i][j]=(c.a[i][j]+cheng(a.a[i][k],b.a[k][j]))%mod; return c; } public:data calc(data a,int b,int K) { data ans,aa=a; ans.data1();ans.x=ans.y=K+1; for (;b;b>>=1,aa=ch(aa,aa)) if(b&1)ans=ch(ans,aa); return ans; } public:ll getans(int K) { for (int now=0;now<=m;++now) { A[now].data1();A[now].x=K+1,A[now].y=K+1; for (int i=1;i<=K;++i)A[now].a[K+1][i]=now%mod; for (int i=1;i<=K;++i) for (int j=1;j<=i;++j) A[now].a[j][i]=1; } B[0].data1();B[0].x=B[0].y=K+1; B[1]=calc(A[0],BB-1,K); for (int i=2;i<=m;++i) { B[i].data1();B[i].x=B[i].y=K+1; for (int j=1;j<=BB-1;++j) { B[i]=ch(B[i],B[i-1]); B[i]=ch(B[i],A[i-1]); } B[i]=ch(B[i],B[i-1]); } for (int i=0;i<=m;++i)B[i]=ch(B[i],A[i]); ans.x=1,ans.y=K+1;memset(ans.a,0,sizeof(ans.a));ans.a[1][K+1]=1; for (int i=top;i>=1;--i)ans=ch(ans,calc(B[i-1],aa[i],K)); return ans.a[1][K]; } public:int countTrailingZeros(int N,int K,int B) { int hh[]={0,0,2,3,2,5,3,7,2,3,5}; int hhh[]={0,0,1,1,2,1,1,1,3,2,1}; BB=hh[B];top=0;while(N){aa[++top]=N%BB;N/=BB;}m=top;ll fans; if(hhh[B]==1){mod=1000000009LL;fans=getans(K);return (int)fans;} else { mod=hhh[B];fans=getans(K); mod=1000000009LL;fans=(getans(K)-fans+mod)%mod; fans=fans*power(hhh[B],mod-2)%mod; return (int)fans; } }};
总结:
1、矩阵满足结合律不满足交换律
2、多次处理不要忘记数组的清空
3、观察性质的能力非常重要,而这只能通过不断的锻炼才能逐渐加强
4、当某一规律、某一规则重复出现的时候要考虑倍增
- Topcoder SRM 569 1000pt
- Topcoder SRM 544 1000pt
- topcoder srm 531 div2 600pt
- TOPCODER/SRM 566 DIVII(250、500、1000题)(1000PT暂未附上代码)
- SRM 596 1000pt
- Topcoder SRM 709 DIV 2 250pt Robofactory solution
- Topcoder SRM 709 DIV 2 500pt Permatchd2 solution
- Topcoder SRM536 div2 1000pt
- TopCoder SRM 474 DIV1 1000
- Topcoder SRM 635 div2 1000
- Topcoder SRM 636 div2 1000
- tc SRM 554 1000pt
- topcoder srm530 div.2 1000pt
- TopCoder SRM205 Div1 1000pt LongPipes
- TopCoder SRM 569 DIV2 Level3: MegaFactorialDiv2
- TopCoder SRM 634 Div2 Problem 1000 - SpecialStrings
- Topcoder SRM 640 Div2 1000(巧妙数学题)
- Topcoder SRM 517 DIV2 1000 CuttingGrass
- 三维感应仪器系数
- 422 tvs选型
- codevs 3304 水果姐逛水果街Ⅰ 解题记录
- js中调用android中的方法
- LintCode(easy)最长上升连续子序列
- Topcoder SRM 569 1000pt
- Java基础复习---对象构造(构造器)
- Maven学习笔记(一)-Maven安装与环境变量配置
- 第七周项目二----建立链串的算法库
- 【Linux4.1.12源码分析】二层报文发送之net_tx_action
- 面向对象开发的几个核心的思想和概念
- event事件对象
- 二叉树的遍历
- linux下一键安装lamp或lnmp