HDU 4861 Couple doubi (number theory)

Source: Internet
Author: User

HDU 4861 Couple doubi (number theory)
HDU 4861 Couple doubi

Question Link

Given k and p, there are k balls. The value of each ball is 1 ^ I + 2 ^ I +... + (p-1) ^ I (mod p) (1 <= I <= k). Now, the two take turns to take the ball. The final sum of the values of the Ball wins, and ask if the first player can win.

Idea: first-hand cannot lose, non-win is equal, so you only need to consider the value of each ball,
By using the ferma's theorem or Euler's theorem, we can easily obtain that the cycle of this function is p-1,

If I is a multiple of p-1, that is, the position of the cyclic section, then each value is 1 and the sum is p-1.

If I is not in the cyclic node position, take an original root g. According to the nature of the original root,GIContains 1 to p-1, so the original formula is equivalentG1M+G2M+G3M...MOD P, This is an proportional series, obtained using the first n terms and formulasGM? (1?GMP? 1)/(1?GM)MOD PUsing the Fermat TheoremGMP? 1MOD P= 1 then the original formula is 0

It is proved that the original cycle is p-1 and only has a value on the cycle section. Therefore, you only need to determine whether the number of the original cycle sections is an odd or even number.

Code:

#include 
 
  #include 
  
   #include 
   
    long long k, p;int main() {    while (~scanf("%lld%lld", &k, &p)) {if (k / (p - 1) % 2) printf("YES\n");else printf("NO\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.