"Chinese remainder theorem" "Tolerant principle" "fast multiplication" "Number theory" HDU 5768 Lucky7

Source: Internet
Author: User
Tags cas

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=5768

Main Topic :

T set of data, the l~r satisfies: 1. is a multiple of 7, 2. The number of%pi!=ai for n primes.

Topic Ideas:

"Chinese remainder theorem" "Tolerant principle" "fast multiplication" "Number theory"

 Because they are prime numbers, they are 22 of each other, satisfying the conditions of the Chinese remainder theorem.

The 7 is added to the prime number, a=0, so as to solve the n+1 equation of the general solution (the smallest solution). Then calculate how many solutions are in l~r.

But because the condition of the Chinese remainder theorem is established at the same time, and the topic is or the relationship, so we should use the principle of tolerance to overlay the limitation.

The middle process may explode long, so use fast multiplication (similar to fast power, just multiply to add)

1 //2 //by Coolxxx3 //4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <memory.h>9#include <time.h>Ten#include <stdio.h> One#include <stdlib.h> A#include <string.h> - //#include <stdbool.h> -#include <math.h> the #defineMin (a) < (b) ( A):(B)) - #defineMax (a) (a) > (b)? ( A):(B)) - #defineABS (a) ((a) >0? ( A):(-(a))) - #defineLowbit (a) (a& (a)) + #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) + #defineEPS (1E-8) A #defineJ 10000000 at #defineMAX 0x7f7f7f7f - #definePI 3.1415926535897 - #defineN 24 - using namespacestd; -typedefLong LongLL; - intCas,cass; in intn,m,lll; - LL L,r,ans; to LL P[n],a[n]; + BOOLU[n]; - ll Cheng (ll a,ll b,ll MoD) the { *LL sum=0; $      for(; b;b>>=1)Panax Notoginseng     { -         if(B&AMP;1LL) sum= (sum+a)%MoD; theA= (a+a)%MoD; +     } A     returnsum; the } +ll EXGCD (ll a,ll b,ll &x,ll &y) - { $     if(!B) {x=1, y=0;returnA;} $LL D=EXGCD (b,a%b,y,x); -y-=a/b*x; -     returnD; the } -LL China (intnn)Wuyi { theLL sum=0, tot=1, Tott,x,y; -     inti; Wu      for(i=1; i<=nn;i++)if(U[i]) tot*=P[i]; -      for(i=1; i<=nn;i++) About     { $         if(!u[i])Continue; -tott=tot/P[i]; - EXGCD (tott,p[i],x,y); -X= (X%p[i]+p[i])%P[i]; ASum= ((Sum+cheng (A[i]*tott%tot,x,tot))%tot+tot)%tot; +     } theSum= (r+tot-sum)/tot-(l1+tot-sum)/tot; -     returnsum; $ } the intMain () the { the #ifndef Online_judge the //freopen ("1.txt", "R", stdin); - //freopen ("2.txt", "w", stdout); in     #endif the     intI,j,k,ii; the //for (scanf ("%d", &cas); cas;cas--) About      for(SCANF ("%d", &cas), cass=1; cass<=cas;cass++) the //while (~scanf ("%s", s)) the //while (~scanf ("%d", &n)) the     { +ans=0; -printf"Case #%d:", Cass); thescanf"%d%lld%lld",&n,&l,&R);Bayi          for(i=1; i<=n;i++) thescanf"%lld%lld",&p[i],&a[i]); theLll=1<<N; -n++; -u[n]=1;p [n]=7; a[n]=0; the          for(i=0; i<lll;i++) the         { the              for(j=i,k=0, ii=1; ii<n;j>>=1, ii++) the             { -u[ii]=j&1; thek+=U[ii]; the             } thek=k&1?-1:1;94ans+=1ll*k*China (n); the         } theprintf"%lld\n", ans); the     }98     return 0; About } - /*101 //102 103 //104 */
View Code

"Chinese remainder theorem" "Tolerant principle" "fast multiplication" "Number theory" HDU 5768 Lucky7

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.