zoukankan      html  css  js  c++  java
  • HNU2[J题]Modified LCS 扩展GCD

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12831&courseid=268

    解题思路:求出最小的两个为正且相等的下标(利用扩展GCD),然后就可以直接求了。

    解题代码:

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 using namespace std;
    13 #define LL __int64
    14 LL x,y,gcd;
    15 void exgcd(LL a ,LL b)
    16 {
    17     if (!b)
    18     {
    19         x=1;
    20         y=1;
    21         gcd=a;
    22         return;
    23     }
    24     exgcd(b,a%b);
    25     LL t=x;
    26     x=y;
    27     y=t-a/b*y;
    28     return ;
    29 }
    30 int main()
    31 {
    32     int T;
    33     LL n1,n2,f1,f2,d1,d2;
    34 //    freopen("in.txt", "r", stdin);
    35 //    freopen("outt.txt","w",stdout);
    36     scanf("%d",&T);
    37     while (T--)
    38     {
    39         cin>>n1>>f1>>d1>>n2>>f2>>d2;
    40         LL f=f2-f1;
    41         exgcd(d1,-d2);
    42         if (f % gcd)
    43         {
    44             puts("0");
    45             continue;
    46         }
    47         else
    48         {
    49             LL r=abs((-d2)/gcd);
    50             x=(x*f/gcd % r+r)%r;
    51             y=(f-x*d1)/(-d2);
    52             LL dx=abs(d1/gcd);
    53             LL dy=abs(d2/gcd);
    54             LL ans=min((n1-x-1)/dy,(n2-y-1)/dx)+1;
    55             //if (ans<0) ans=0;
    56             cout<<ans<<endl;
    57         }
    58     } 
    59     return 0;
    60 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    PHP 文件上传
    浅析文件上传漏洞
    JS之Number类
    JS之String类
    Java中的多态
    JS之数据类型
    JavaScript的组成
    双向链表与LRU算法实现
    字符串比较--小问题大智慧
    龙生九子-浅谈Java的继承
  • 原文地址:https://www.cnblogs.com/zyue/p/3859724.html
Copyright © 2011-2022 走看看