hdu---1719Friend

来源:互联网 发布:专业淘宝拍摄 编辑:程序博客网 时间:2024/05/01 17:56

//是一个由题设条件,推导公式的题目, 一开始有点想法,但知道肯定超时。

//后来参看了傅总的结题报告,下面是其转载。   学习学习!

hdu-1719 Friend  

2011-07-25 14:50:47|  分类: ACM-hdu|举报|字号 订阅

 
 
Friend

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 757 Accepted Submission(s): 353

Problem Description

Friend number are defined recursively as follows.
(1) numbers 1 and 2 are friend number;
(2) if a and b are friend numbers, so is ab+a+b;
(3) only the numbers defined in (1) and (2) are friend number.
Now your task is to judge whether an integer is a friend number.

 

 

Input

There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.

 

 

Output

For the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.

 

 

Sample Input

 

3

13121

12131

 

 

Sample Output

 

YES!

YES!

NO!

 

————————————————————————————————————————————————

解题思路:

 

这是一道想法题!因为任意一个friend number可以由两个friend numbera,b构成,且为ab+b+a;

想法就是在这个表达式中!

因为 ab+a+b=ab+a+b+1-1=(a+1)*(b+1)-1

假设 a=(c+1)*(d+1)-1;b=(e+1)*(f+1)-1;

得到 ab+a+b=(c+1)*(d+1)*(e+1)*(f+1)-1

带入数值化简就得到,ab+a+b=((1+1)^x)*((1+2)^y)-1(x,y >=0)【也就是说c,d,e,f也可以看成是a和b,找到他们的c,d,e,f,找到最后,c,d,e,f一定能够用a和b来表示,所以代入1和2,得到这样的等式】

就把题目转化为一个数如果过能写成 (2^x)*(3^y)-1 那他就是一个friend number!

—————————————————————————————————————————————————

Code:

#include<iostream>using namespace std; int main(){ #ifndef ONLINE_JUDGEfreopen("testin.txt","r",stdin); #endifint a; while(scanf("%d",&a)!=EOF){ if(!a) printf("NO!\n"); else { a++; while(a%2==0) a=a/2; while(a%3==0) a=a/3; if(a==1) printf("YES!\n"); else printf("NO!\n");}} return 0;}

 

------------------转载自 fudq

0 0
原创粉丝点击