原文地址: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1003&cid=843
Problem Description
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
鸽子数字由以下过程定义:从任何正整数开始,将数字替换为其各个数位的平方和,并重复该过程,直到该数字等于1。如果不能,则这个数字不是鸽子数。
例如7是鸽子数,因为7->49->97->130->10->1。(7*7=49,4*4+9*9=97,9*9+7*7=130....如此类推)
显然1是第一个鸽子数。
有Q个询问,每个询问给出一个数k,你需要输出第k个鸽子数。
Input
第一行一个Q,代表询问的个数(Q<=100000)
接下来Q行,每行一个数字k(k<150000)
Output
每行输出一个数,代表第k个鸽子数
Sample Input
2
1
2
Sample Output
1
7
又是鸽子数,首先需要先写一下判断鸽子数的代码
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1e9+7;
const int inf=1e7;
ll a[150010];
deque <ll> dq;
int main()
{
ll k,i,j,sum=0,flag=0,ans,e=0;
cin>>k;
ll kk=k;
while(1)
{
sum=0;
if(e>=100)
break;
while(1)
{
if(kk==0)
break;
sum+=(kk%10)*(kk%10);
kk/=10;
}
//cout<<sum<<endl;
if(sum==1)
{
flag=1;
break;
}
kk=sum;
sum=0;
e++;
}
if(flag==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
写好了这一步,就很容易做这个题了,数据量大,很容易时间超限。~~~又是一个开不了数组的题目。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=1e9+7;
ll a[150010]={0};
deque <ll> dq;
void f()
{
ll i,j,k;
ll p=1;
for(k=1; p<150000; k++)//注意是p<150000,因为k的范围不能确定。
{
ll kk=k;
ll sum=0,flag=0,ans,e=0;
while(1)
{
sum=0;
if(e>=20)//如果是e>=100会时间超限
break;
while(1)
{
if(kk==0)
break;
ll h=kk%10;
sum+=h*h;
kk/=10;
}
if(sum==1)
{
flag=1;
break;
}
kk=sum;
sum=0;
e++;
}
if(flag==1)
a[p++]=k;
}
}
int main()
{
ll T,n,i;
cin>>T;
f();
while(T--)
{
cin>>n;
cout<<a[n]<<endl;
}
return 0;
}