UVA-10710 skyscraper floors (find the Law + power to take the mold)

Source: Internet
Author: User

Topic: The topic gives a definition of a number, according to the definition, let judge whether a given number is not this number. In the question called the number of Jimmy Number, defined as follows: To sequence 1,2,3,,,,n, do n-1 SF transformation (interpretation of the transformation in the following), if the original sequence can be obtained, then n is the number of Jimmy.

SF Transform: If n is an even number, take n=10 as an example, once the SF transformation is this

1,2,3,4,5,6,7,8,9,10->1,2,3,4,5 6,7,8,9,10->6,1,7,2,8,3,9,4,10,5 This is an even-numbered SF transformation, the second transformation is based on a new sequence, and so on.

If n is an odd number, take n=9 as an example, the one-time SF transform is like this

1,2,3,4,5,6,7,8,9->1,2,3,4 5,6,7,8,9->5,1,6,2,7,3,8,4,9 This is an odd-numbered SF transformation, and the second transformation is based on a new sequence, and so on.

Title Analysis: According to the tips in the title, even the number is not Jimmy. In the case of a Jimmy number, the position period of any element in the corresponding sequence is the same, so just consider 1. Finally found 1 of the position has the following rules: Pos= (2^t)%n, where T is the number of transformations. So just determine if (2^ (n-1))%n=1 is set up.

In fact, the power to take the mold!!!

The code is as follows:

1# include<iostream>2# include<cstdio>3# include<cstring>4# include<algorithm>5 using namespacestd;6# define LLLong Long7 ll Mypow (ll a,ll b,ll m)8 {9     if(b==0)Ten         return 1; One     if(b==1) A         returna%m; -ll U=mypow (a,b/2, m); -u*=u; theu%=m; -     if(b&1) -u*=A; -     returnu%m; + } - BOOL  is(ll x) + { All Pos=mypow (2, X-1, x); at     returnpos==1; - } - intMain () - { - ll N; -      while(SCANF ("%lld", &n) &&n!=-1){ in         if( is(n)) -printf"%lld is a jimmy-number\n", n); to         Else +printf"%lld is not a jimmy-number\n", n); -     } the     return 0; *}
View Code

UVA-10710 skyscraper Floors (find rule + power modulo)

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.