HDU 1719 Friend Mathematical deduction

Source: Internet
Author: User

Chain: http://acm.hdu.edu.cn/showproblem.php?pid=1719

FriendTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2099 Accepted Submission (s): 1058


Problem descriptionfriend number is 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) is friend number.
Now your task was to judge whether an integer is a friend number.

Inputthere is several lines in input with each line have a nunnegative integer a, 0<=a<=2^30.

Outputfor the number A on all line of the input, if a is a friend number, output "yes!", otherwise output "no!".

Sample Input
31312112131

Sample Output
yes! Yes!no!



Test instructions: 1 and 2 are friendly numbers, and if a, B is a friendly number, then A*b+a+b is also a friendly number.

Practice:

friend=a*b+a+b=a*b+a+b+1-1= (a+1) * (b+1)-1

Suppose a= (c+1) * (d+1)-1 b= (e+1) * (f+1)-1

Then friend= (c+1) * (d+1) * (e+1) * (f+1)-1

Then you can put a friend number through this, constantly into the decomposition, and finally must be (c+1) * (d+1) * (e+1) * (f+1) * (g+1) * (h+1) * (i+1) *.....-1 This form, decomposition to the end, that is, the letters are all 1 or 2.

Then can get any one friend=2^n+3^m-1;

So after a number plus 1, if factorization is only 2 and 3, then this number is the friend number.


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 999999999#define EPS 0.00001#define LL __int64 #define PI ACOs ( -1.0) int main () {int n;while (scanf ("%d", &n)!=eof) {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"); elseprintf ("no!\n");} return 0;} /*4 12 3 4 11 2 3 4*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1719 Friend Mathematical deduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.