Sha 10710-Chinese shuffle (perfect shuffling)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10710-Chinese shuffle

Question: give n cards, arrange them in order, perform the n-1 shuffling perfectly, and ask if they can be changed to the original German sequence.

Solution: according to the nature of the perfect shuffling, the X card after the P times has X? 2 P, so you only need to prove whether the 1st cards are in the distance.

#include <cstdio>#include <cstring>typedef long long ll;ll pow_mod(ll a, ll n, ll mod) {    ll ans = 1;    while (n) {        if (n&1)            ans = ans * a % mod;        a = a * a % mod;        n /= 2;    }    return ans;}int main () {    ll n;    while (scanf("%lld", &n) == 1 && n != -1) {        if (pow_mod(2, n-1, n) == 1)            printf("%lld is a Jimmy-number\n", n);        else            printf("%lld is not a Jimmy-number\n", n);    }    return 0;}

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.