"BZOJ1951" "SDOI2010" Ancient pig Lucas theorem, Chinese remainder theorem, exgcd, Fermat theorem

Source: Internet
Author: User

Description "There's a bunch of little pigs on the other side of the mountain. They are lively and clever, they are naughty and sensitive. They live freely in the green lawn, they are kind and brave to each other care ... "--selected from the Pig King National song Long Long ago, in the mountains over yonder sea side of a piece of feng shui treasure there once existed a pig kingdom. The Pig kingdom is in a remote location, with the implementation of the manor economy that adapts to the self-sufficiency of the society at that time, with little contact with the outside world and less business activities. So there are very few other animals that know such a kingdom. Although the Pig kingdom is not big, but the land is fertile, houses. If you must take anything compared with it, it is only the Eastern Jin Dynasty Tao Yuanming's imagination in the dream of the peach blossom. The Pig king diligent, the pig people live in peace and harmony, the country is orderly, the economy is flourishing, the society is harmonious and stable. The Harmonious society brings the pig people's enthusiasm for the work fiery and the pink vision of the future. Piglet Ipig is a very ordinary citizen of the Pig kingdom. The pig is 10 years old, in the Big Fat Pig School in the third grade of primary school. Like most pigs, he is not very clever, so he often encounters a lot of things that are strange or so easy for others to make him very troubled. The pig later took part in the whole Swine Informatics Olympiad (Pig Olympiad in Informatics, POI), made a good position, and eventually walked into the Pig Kingdom University (Pig Kingdom University, PKU) for further study. Now the piglet has been able to use the computer to solve the simple problem, such as can be written in p++ language program to calculate the value of a + B. This "achievement" has become a topic he has talked about. Of course, the students who do not understand the truth began to take notice of him ~ The story of the pig will start from this, along with everyone two days, I hope you can like the pig. The topic describes the civilization of the Pig kingdom has a long history, profound. Ipig in the Big Fat Pig School library to check the information, learned that ancient times the total number of pig text is n. Of course, a language if a lot of words, the dictionary will be very large. At that time, the king of the Pig kingdom considered that if a dictionary was repaired, the scale could be far more than the Kangxi Dictionary, the cost of the pig power, the material will be difficult to measure. It is therefore considered that the exhausting of the pig was not carried out repeatedly. Of course, the words of the Pig kingdom were later simplified with historical changes, and some less commonly used words were removed. Ipig intends to study the pig text of a dynasty in ancient times. According to the relevant literature, the pig text that was circulated in that dynasty was one of the k fractions of the ancient times, where K was a positive approximate of n (1 and N). But exactly what is one of the K, and K is how much, because the history is too long, has not been verified. Ipig think that as long as the literature, each can divide n k is possible. He intends to take into account all possible K. Obviously, when k equals a certain value, the number of characters of the pig in the face is n/k.。 However, it is also quite a lot to keep n/k from n characters. Ipig predicts that if all the possible k's cases add up to p, then the cost of studying ancient writings will be the P-th side of G. Now he wants to know what the cost of studying ancient writings in the Pig Kingdom is. Since Ipig thinks this number can be astronomical, you just need to tell him that the answer is divided by the remainder of 999911659. Input has and has only one line: two numbers n, G, separated by a space. Output has and has only one row: A number that represents the remainder of the answer divided by 999911659. Sample Input4 2
Sample Output2048

10% of the data, 1 <= N <= 50;
20% of the data, 1 <= N <= 1000;
40% of the data, 1 <= N <= 100000;
100% of the data, 1 <= G <= 1000000000,1 <= N <= 1000000000.


Sdoi2010 Contest2

Does not use the mathematical formula compiler 23333~, looked at the question surface on the vexed series, test instructions: Given g,n, asks G^m (mod P), M=∑c (d,n) (d|  N).  1, g^m (mod p) =g^ (M mod p-1)-Fermat theorem, G and p are coprime cases, when g==p should output 0 instead of 1, there is a set of data cards. 2, then the problem is converted into ∑c (d,n) (d| N) (mod P-1), however, p-1=999911658 is not prime, the qualitative factor decomposition is 999911658=2*3*4679*35617, just a prime number spicy, then, then Lucas theorem directly set up Ah, so that constitutes 4 a congruence equation,  You can use the rest of China to understand spicy.  3, the last fast power. 4, 1A good cool, but the card the longest time is a long long problem, burst int I am unaware of the 2333.
1#include <iostream>2#include <cstdio>3#include <cmath>4 #defineP 9999116595 #definell Long Long6 using namespacestd;7ll p[4]={2,3,4679,35617};8ll fac[4][40010],m[4];9 ll N,g;Ten ll Qpow (ll a,ll b,ll c) One { A     intans=1; -      while(b) -     { the         if(b&1) ans=ans*a%C; -a=a*a%C; -b>>=1; -     } +     returnans; - } +   A ll C (ll m,ll n,ll t) at { -     if(n<m)return 0; -     returnFac[t][n]*qpow (fac[t][n-m]*fac[t][m],p[t]-2, P[t])%P[t]; - } -   - ll Lucas (ll m,ll N,ll t) in { -     if(m==0)return 1; to     returnC (m%p[t],n%p[t],t) *lucas (m/p[t],n/p[t],t)%P[t]; + } -   the voidEX_GCD (ll a,ll b,ll &x,ll &y) * { $     if(b==0) {x=1; y=0;return;}Panax NotoginsengEX_GCD (b,a%b,x,y); -     intT=x; X=y; y=t-a/b*y; the } +   A ll Chinese_remainer () the { +ll re=0, ree=0; -      for(intI=0;i<4; i++) $     { $ ll x, y; -EX_GCD (P-1)/p[i],p[i],x,y); -Ree= (Percent (p1) * (P-1) (/p[i])% (P-1) + (P-1))% (P-1); there+=ree*m[i]%999911658; -re%=999911658;Wuyi     } the     returnre; - } Wu   - intMain () About { $scanf"%d%d",&n,&G); -      for(intI=0;i<4; i++) -     { -fac[i][0]=1; A          for(intj=1; j<=p[i];j++) +Fac[i][j]= (fac[i][j-1]*J)%P[i]; the     } -     if(g==p) {printf ("0\n");return 0;} $g%=P; the      for(intI=1; I<=sqrt (N); i++) the     { the         if(n%i==0) the         { -             intt=n/i; in              for(intj=0;j<4; j + +) the             { theM[j]= (M[j]+lucas (i,n,j))%P[j]; About                 if(t!=i) m[j]= (M[j]+lucas (t,n,j))%P[j]; the             } the         } the     } +     intans=Chinese_remainer (); -printf"%d\n", Qpow (g,ans,p)); the     return 0;Bayi}
View Code

"BZOJ1951" "SDOI2010" Ancient pig Lucas theorem, Chinese remainder theorem, exgcd, Fermat theorem

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.