码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-1719 Friend 数学推导

时间:2019-09-30 14:40:25      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:mode   typedef   scan   ssi   eps   you   only   eve   tags   

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.

InputThere are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
OutputFor 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!


OJ-ID:
HDU-1719

author:
Caution_X

date of submission:
20190930

tags:
数学推导

description modelling:
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.

major steps to solve it:
1.设n是Friend数,则n=ab+a+b=(a+1)*(b+1)-1   =>   n+1=(a+1)*(b+1)
2.a,b都是Friend数,所以a+1=(a‘+1)*(b‘+1),b+1=(a‘‘+1)*(b‘‘+1)直到a,b=1,2
3.那么:n+1= 2^x * 3^y

warnings:
0需要特判

AC CODE:
技术图片
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n;
    while(~scanf("%lld",&n)){
        if(n==0){
            printf("NO!\n");
            continue;
        }
        n++;
        while(n%2==0)    n/=2;
        while(n%3==0)    n/=3;
        if(n==1)    printf("YES!\n");
        else    printf("NO!\n");
    }
    return 0;
}
View Code

 




HDU-1719 Friend 数学推导

标签:mode   typedef   scan   ssi   eps   you   only   eve   tags   

原文地址:https://www.cnblogs.com/cautx/p/11612438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!