HDU 5768 Lucky7 (Tolerant principle + Chinese remainder theorem + state compression + Band-film multiplication)

Source: Internet
Author: User
Tags mul

Test instructions: ... I don't have to say it, it seems to be a very tolerant principle, the Chinese remainder theorem .

Method: Because the number of N in the topic is 15, using State compression can lift all the combinations, and then open the number of groups, the Chinese remainder theorem, the Chinese remainder theorem can be found at the same time to meet the residual film conditions of the smallest integer x,x in (1,m) by a unique value, M is the product of each divisor, all the solutions that meet the conditions are ans = x+k*m, you can know in [1,r] This interval, there is (m+r-x)/M K meet the conditions, and then in order to prevent overflow in the operation, so the use of the Band-film multiplication , is to convert the multiplier into binary, With the displacement operator, the film is constantly being taken in the middle of the process (see the code is easy to understand)

Note: In order to simplify the operation, add (7,0) This group, with the film multiplication, you need to use the same residual film theorem to convert the multiplier to an integer, because the Euclidean algorithm may return negative numbers, do not convert into a dead loop, I forgot, the result ... The examples given are already dead in the inside ...

Sentiment: Really deserves to be a multi-school training tournament, a topic fused so much knowledge points.

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineN 20#defineLL Long Longintn,cnt; LL X,y,p[n],a[n],pt[n],at[n];intGet_zuhe (intk) {    intIP; CNT= IP =0;  while(k) {if(k&1) {pt[cnt]=P[ip]; AT[CNT]=A[ip]; CNT++; } IP++; K>>=1; } pt[cnt]=7; AT[CNT]=0; CNT++; return(cnt%2);} ll EX_GCD (ll A,ll b,ll&x,ll &y) {    if(b==0) {x=1; y=0; returnA; } LL R= EX_GCD (b,a%b,y,x); Y-= x* (A/b); returnR;} ll Mul (ll x,ll y,ll M) {ll ans=0;  while(y) {//cout<<y<<endl;        if(y&1) ans = (ans+x%m)%l; X= (x + x)%M; Y>>=1; }    returnans;} LL China () {ll M=1, M,ret =0, X,y,l,r;  for(inti =0; i < cnt;i++) M *=Pt[i];  for(inti =0; I < cnt;i++) {m= m/Pt[i];        EX_GCD (M,pt[i],x,y); X= (x+m)%M;///don't forget to convert to positiveret = (Ret+mul (Mul (m,x,m), at[i],m)%M)%M; } ret= (ret+m)%M; //printf ("m =%i64d\n", m); //printf ("ret =%i64d\n", ret);R = (Y+m-ret)/M; L= (X-1+m-ret)/M; returnRL;} LL Solve () {intTMP = (1<<N), judge; LL All= y/7-(X-1)/7; LL sum=0, ch;  for(inti =1; I < tmp;i++) {Judge=Get_zuhe (i); CH=China (); //printf ("china[%d] =%i64d\n", i,ch);        if(judge) Sum-=ch; ElseSum + =ch; }    return(All-sum);}intMain () {//freopen ("A.in.cpp", "R", stdin);    intT,ca =0; scanf ("%d",&t);  while(t--) {scanf ("%d%i64d%i64d",&n,&x,&Y);  for(inti =0; I < n;i++) {scanf ("%i64d%i64d",&p[i],&A[i]); } printf ("Case #%d:%i64d\n",++ca,solve ()); }    return 0;}

HDU 5768 Lucky7 (Tolerant principle + Chinese remainder theorem + state compression + Band-film multiplication)

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.