1213 number of solutions [one in the theory of numbers with the church practice]

Source: Internet
Author: User

1213 number of solutions

time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingView Run ResultsTitle Description Description

The known integer x,y satisfies the condition as follows:

Ax+by+c = 0

P<=x<=q

R<=y<=s

The number of x,y that satisfies these conditions.

Enter a description Input Description

The first line has an integer n(n<=10), which indicates that there are n tasks. n<=10

The following are n rows with 7 integers per line:a,b,c,p,Q,R,s. are not more than 108.

Output description Output Description

A total of n rows , and the first row is the number of solutions for the I task.

Sample input Sample Input

2

2 3-7 0 10 0 10

1 1 1-10) 10-9 9

Sample output Sample Output

1

19

Data range and Tips Data Size & HintCategory labels Tags Click here to expandEuclidean theorem number theory Two types of AC code: 1, expand Euclidean algorithm, need to consider special data
#include <iostream>#include<algorithm>#include<math.h>using namespaceStd;typedefLong Longll;Doublez[Ten];voidEXGCD (ll a,ll b,ll &d,ll &x,ll &y) {    if(!B) {d=a;x=1; y=0;return;} EXGCD (B,a%B,D,Y,X); y-=x* (A/b);}intMain () {ll a,b,c,p,q,r,s,x,y,d,k; intN;cin>>N;  while(n--) {cin>>a>>b>>c>>p>>q>>r>>s;c=-C; if(p>q| | r>s| | (!a&&!b&&c)) {cout<<0<<endl;Continue;} if(!a| |!b) {            if(!a) a=q-p+1; Else if(c%a==0&& (C/A) <=q&& (C/A) >=p) a=1; ElseA=0; if(!b) b=s-r+1; Else if(c%b==0&& (c/b) <=s&& (c/b) >=r) b=1; Elseb=0; cout<<a*b<<Endl; Continue;        } exgcd (A,b,d,x,y); if(c%d!=0) {cout<<0<<endl;Continue;} K=c/d;x*=k;y*=K; A=a/d;b=b/D; z[0]= (q-x) *1.0/b; z[1]= (p-x) *1.0/b; z[2]= (y-s) *1.0/A; z[3]= (Y-R) *1.0/A; Sort (z,z+4); A=floor (z[2]); B=ceil (z[1]); cout<<a-b+1<<Endl; }    return 0;}

2. Direct Enumeration

#include <cstdio>#include<iostream>#definell Long Longusing namespacestd;ll N,a,b,c,p,q,r,l,ans;intMain () {CIN>>N;  for(intI=1; i<=n;i++) {cin>>a>>b>>c>>p>>q>>l>>R; Ans=0; if(b==0){             for(intx=p;x<=q;x++){                if(x*a==-c) ans++; } cout<<ans* (r-l+1) <<Endl; }        Else{             for(intx=p;x<=q;x++){                if((-c-a*x)%b==0&& (-c-a*x) *1.0/b>=l&& (-c-a*x) *1.0/B&LT;=R) ans++; } cout<<ans<<Endl; }    }    return 0;}

1213 number of solutions [one in the theory of numbers with the church practice]

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.