zoukankan      html  css  js  c++  java
  • 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接:

      http://codeforces.com/problemset/problem/710/D

    题目大意:

      两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个。

      0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R).

    题目思路:

      【数论】【扩展欧几里得】

      据题意可得同余方程组  x=b1(mod a1)  即  x=k1*a1+b1

                  x=b2(mod a2)     x=k2*a2+b2

      化简,k1*a1=k2*a2+(b2-b1) 即 a1= (b2-b1)(mod a2)

      于是只要求一个同余方程即可。令a=a1,b=a2,c=b2-b1。

      扩展欧几里得求解x,再把x改为在L~R区间内的第一个通解,计算数量即可(每次增加lcm(a1,a2)答案+1)。

      

     1 //
     2 //by coolxxx
     3 //#include<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<map>
     9 #include<memory.h>
    10 #include<time.h>
    11 #include<stdio.h>
    12 #include<stdlib.h>
    13 #include<string.h>
    14 //#include<stdbool.h>
    15 #include<math.h>
    16 #define min(a,b) ((a)<(b)?(a):(b))
    17 #define max(a,b) ((a)>(b)?(a):(b))
    18 #define abs(a) ((a)>0?(a):(-(a)))
    19 #define lowbit(a) (a&(-a))
    20 #define sqr(a) ((a)*(a))
    21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    22 #define mem(a,b) memset(a,b,sizeof(a))
    23 #define eps (1e-8)
    24 #define J 10
    25 #define mod 1000000007
    26 #define MAX 0x7f7f7f7f
    27 #define PI 3.14159265358979323
    28 #define N 20000004
    29 using namespace std;
    30 typedef long long LL;
    31 int cas,cass;
    32 int n,m,lll,ans;
    33 LL a1,a2,b1,b2,l,r;
    34 LL exgcd(LL a,LL b,LL &x,LL &y)
    35 {
    36     if(!b){x=1,y=0;return a;}
    37     LL d=exgcd(b,a%b,y,x);
    38     y-=a/b*x;
    39     return d;
    40 }
    41 int main()
    42 {
    43     #ifndef ONLINE_JUDGE
    44 //    freopen("1.txt","r",stdin);
    45 //    freopen("2.txt","w",stdout);
    46     #endif
    47     int i,j,k;
    48     LL a,b,c,d,x,y,ny,lcm;
    49 //    for(scanf("%d",&cas);cas;cas--)
    50 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    51 //    while(~scanf("%s",s+1))
    52     while(~scanf("%I64d",&a1))
    53     {
    54         cin>>b1>>a2>>b2>>l>>r;
    55         l=max(l,b1);l=max(l,b2);
    56         d=exgcd(a1,a2,x,y);
    57         lcm=a1/d*a2;
    58         if((b2-b1)%d!=0)
    59         {
    60             puts("0");
    61             continue;
    62         }
    63         a=a1/d;b=a2/d;c=(b2-b1)/d;
    64         d=exgcd(a,b,x,y);
    65         x=a1*(x*c)+b1;
    66         if(x>l)x=x-(x-l)/lcm*lcm;
    67         else x=x+(l-1-x+lcm)/lcm*lcm;
    68         if(x>r)puts("0");
    69         else printf("%I64d
    ",(r-x+lcm)/lcm-(l-1-x+lcm)/lcm);
    70     }
    71     return 0;
    72 }
    73 /*
    74 //
    75 
    76 //
    77 */
    View Code
  • 相关阅读:
    epplus
    http://www.cmake.org/
    patheditor2
    emgu cv
    OpenCV
    Tesseract
    Blendar基金会
    安装服务windows,installutil
    R 软件
    历史为什么选择C语言?事实证明:暮年的C语言,依旧宝刀未老!
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5799650.html
Copyright © 2011-2022 走看看