URAL 1009/URAL 1012/URAL 1013
2013-08-21 16:02
447 查看
题目大意:求N位K进制数中没有两个相邻0的数的个数。
1009 Time Limit:1000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
1012Time Limit:1000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
1013 Time Limit:2000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
数据规模:
1009:2<=K<=10,N>=2,N+K<=18。
1012:2<=K<=10,N>=2,N+K<=180。
1013:2<=K<=10,N>=2,N+K<=1800。
理论基础:K进制数的意义。
题目分析:我们用ans[i]表示i位时满足条件的K进制数的个数。
首先,我们可以知道,ans[1]=K-1。然后考虑给ans[1]上添加数字,而对于每个K进制数,它的最高位只能有K-1中可能,而此时低位可以有K种可能,所以易得:ans[2]=(K-1)*K。
然后,我们可以观察到:ans[i]的所有数的最高位都不可能是0,而且符合题设要求,所以可以给ans[i]加上一个最高位得到ans[i+1]。可别忘了,此时ans[i+1]的第i位可以为0呀,那么因为加上的最高位的数字不为0,所以只需要ans[i+1]的第i位以后的数满足要求即可,而这样的数的个数正是ans[i-1]。
最后,我们可以得出:ans[i]=(ans[i-1]+ans[i-2])*(K-1)。
所以,对于1009,最后的答案不会超过long long,所以可以轻松解决。
而对于1012与1013,我们可以按照1013对待只需要编写高精度即可。或者使用JAVA。即可解决。
代码如下:
1009:
by:Jsun_moon http://blog.csdn.net/jsun_moon
1009 Time Limit:1000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
1012Time Limit:1000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
1013 Time Limit:2000MS Memory
Limit:16384KB 64bit IO Format:%I64d
& %I64u
数据规模:
1009:2<=K<=10,N>=2,N+K<=18。
1012:2<=K<=10,N>=2,N+K<=180。
1013:2<=K<=10,N>=2,N+K<=1800。
理论基础:K进制数的意义。
题目分析:我们用ans[i]表示i位时满足条件的K进制数的个数。
首先,我们可以知道,ans[1]=K-1。然后考虑给ans[1]上添加数字,而对于每个K进制数,它的最高位只能有K-1中可能,而此时低位可以有K种可能,所以易得:ans[2]=(K-1)*K。
然后,我们可以观察到:ans[i]的所有数的最高位都不可能是0,而且符合题设要求,所以可以给ans[i]加上一个最高位得到ans[i+1]。可别忘了,此时ans[i+1]的第i位可以为0呀,那么因为加上的最高位的数字不为0,所以只需要ans[i+1]的第i位以后的数满足要求即可,而这样的数的个数正是ans[i-1]。
最后,我们可以得出:ans[i]=(ans[i-1]+ans[i-2])*(K-1)。
所以,对于1009,最后的答案不会超过long long,所以可以轻松解决。
而对于1012与1013,我们可以按照1013对待只需要编写高精度即可。或者使用JAVA。即可解决。
代码如下:
1009:
#include<iostream> #include<cstring> #include<string> #include<cstdlib> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<ctime> #include<vector> using namespace std; typedef double db; #define DBG 1 #define maa (1<<31) #define mii ((1<<31)-1) #define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试 #define dout DBG && cout << __LINE__ << ">>| " #define pr(x) #x"=" << (x) << " | " #define mk(x) DBG && cout << __LINE__ << "**| "#x << endl #define pra(arr, a, b) if(DBG) {\ dout<<#arr"[] |" <<endl; \ for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \ if((b-a+1)%8) puts("");\ } template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; } template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; } typedef long long LL; typedef long unsigned int LU; typedef long long unsigned int LLU; LL ans[30]; int k,n; int main() { while(~scanf("%d%d",&n,&k)) { ans[1]=k-1; ans[2]=(k-1)*k; for(int i=3;i<=n;i++) { ans[i]=(k-1)*(ans[i-1]+ans[i-2]); } printf("%I64d\n",ans ); } return 0; }1013/1012:
#include<iostream> #include<cstring> #include<string> #include<cstdlib> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<ctime> #include<vector> using namespace std; typedef double db; #define DBG 1 #define maa (1<<31) #define mii ((1<<31)-1) #define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试 #define dout DBG && cout << __LINE__ << ">>| " #define pr(x) #x"=" << (x) << " | " #define mk(x) DBG && cout << __LINE__ << "**| "#x << endl #define pra(arr, a, b) if(DBG) {\ dout<<#arr"[] |" <<endl; \ for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \ if((b-a+1)%8) puts("");\ } template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; } template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; } typedef long long LL; typedef long unsigned int LU; typedef long long unsigned int LLU; #define N 1800 typedef struct bign { short digit[N+1]; int lenth; void valid() { int i=0,n=lenth-1; while(i<n) { if(digit[i]>=100) { digit[i+1]=digit[i]/100+digit[i+1]; digit[i]=digit[i]%100; } i++; } while(digit[i]) { if(digit[i]>=100) { digit[i+1]=digit[i]/100; digit[i]=digit[i]%100; lenth++; } i++; } } bign(){memset(digit,0,sizeof(digit));lenth=1;} bign operator = (bign a) { lenth=a.lenth; for(int i=0;i<a.lenth;i++) { digit[i]=a.digit[i]; } return *this; } bign operator = (int a) { digit[0]=a; valid(); return *this; } }Ans; Ans ans[N+1]; int k,n; Ans add(Ans a,Ans b) { Ans ans; int high=max(a.lenth,b.lenth); ans.lenth=high; for(int i=0;i<high;i++) { ans.digit[i]=a.digit[i]+b.digit[i]; } ans.valid(); return ans; } Ans multiply(int n,Ans f) { Ans ans; ans.lenth=f.lenth; for(int i=0,i_b=f.lenth;i<i_b;i++) { ans.digit[i]=f.digit[i]*n; } ans.valid(); return ans; } int main() { while(~scanf("%d%d",&n,&k)) { ans[1]=k-1; ans[2]=(k*(k-1))%100; for(int i=3;i<=n;i++) { ans[i]=multiply(k-1,add(ans[i-1],ans[i-2])); } for(int j=ans .lenth-1;j>=0;j--) { (j==ans .lenth-1)?printf("%d",ans .digit[j]):printf("%02d",ans .digit[j]); } printf("\n"); } return 0; }其中,高精度的编写也可以用字符串,具体可以自己实现。
by:Jsun_moon http://blog.csdn.net/jsun_moon
相关文章推荐
- URAL 1009|URAL 1012|URAL 1013|K-based Numbers|高精度|动态规划
- ural 1009 1012 1013 K-Based Numbers V1.0/2.0/3.0
- 关于杭州电子科技大学的acm1002, 1003, 1004, 1005, 1008, 1009, 1012, 1013,1014,1015,1016,1017,1019,1020,1021,1022
- URAL - 1012 DP+高精度
- ural1009(动态规划)
- URAL1009 K-based Numbers
- ural 1009
- URAL 1009 K-based Numbers (DP动态规划)
- URAL - 1013 DP+高精度+滚动数组
- ural 1013 K-based Numbers. Version 3(升级版)
- Ural 1009 K-based Numbers(DP)
- URAL 1013 K-based Numbers. Version 3(矩阵快速幂+大数)
- ural 1012
- URAL 1009 K-based Numbers (DFS)
- URAL 1013 K-based Numbers. Version 3
- 递推DP URAL 1009 K-based Numbers
- ural 1009 K-based Numbers
- ural 1013
- K-based Numbers (URAL 1009)
- Ural 1009 K-based Numbers