您的位置:首页 > 其它

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:

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息