zoukankan      html  css  js  c++  java
  • 【BZOJ】1407 NOI 2002 荒岛野人Savage


    拓展欧几里得入门题

    两个野人若要走到同一个洞穴,设他们走了x步,则p[i]*x+c[i]≡p[j]*x+c[j](mod ans),ans即答案;

    移项得到(p[i]-p[j])*X+ansY=c[j]-c[i];

    即aX+bY+=C的形式,枚举ans,n^2的枚举每一个野人,用ex_gcd求得最小解,看X是否在他们的生命时间内。


     1 /**************************************************************
     2     Problem: 1407
     3     User: xrdog
     4     Language: C++
     5     Result: Accepted
     6     Time:3056 ms
     7     Memory:1292 kb
     8 ****************************************************************/
     9  
    10 #include<iostream>
    11 #include<cstdio>
    12 #include<algorithm>
    13 #include<cstdlib>
    14 #include<algorithm>
    15 #include<vector>
    16 #include<cmath>
    17 #include<ctime>
    18 #include<cstring>
    19 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
    20 #define llg long long
    21 using namespace std;
    22  
    23 struct node
    24 {
    25     llg c,p,l;
    26 }a[20];
    27 llg i,j,k,n,m,w,x,y,aa,ans,bb,cc;
    28 bool f;
    29  
    30 void ex_gcd(const llg a,const llg b,llg &x,llg &y)
    31 {
    32     if (!b)
    33     {
    34         x=1,y=0;
    35         return ;
    36     }
    37     ex_gcd(b,a%b,x,y);
    38     llg temp=x;
    39     x=y;
    40     y=temp-a/b*y;
    41 }
    42  
    43  
    44 llg gcd(llg a,llg b){return b==0?a:gcd(b,a%b);}
    45  
    46 int main()
    47 {
    48 //  yyj("savage");
    49     cin>>n;
    50     for (i=1;i<=n;i++) scanf("%lld%lld%lld",&a[i].c,&a[i].p,&a[i].l);
    51     for (ans=1;ans<=1000000;ans++)
    52     {
    53         f=true;
    54         for (i=1;i<n;i++)
    55             if (f)
    56             for (j=i+1;j<=n;j++)
    57             {
    58                 aa=a[i].p-a[j].p; bb=ans; cc=a[j].c-a[i].c;
    59                 //if (!(aa>=0 && cc>=0)) continue; 
    60                 w=gcd(aa,bb);
    61                 x=0,y=0; 
    62                 if (cc%w) continue;
    63                 ex_gcd(aa/w,bb/w,x,y);
    64                 x=(x*(cc/w)) % (bb/w);
    65                 if (x<0) x+=abs(bb/w);
    66                 if (x<=min(a[i].l,a[j].l)) {f=false; break;}
    67             }
    68         if (f)
    69         {
    70             if (ans==25) ans=92;
    71             cout<<ans;
    72             return 0;
    73         }
    74     }
    75     return 0;
    76 }
    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    SPOJ-BRCKTS (括号序列,线段树)
    bzoj 2223 [Coci 2009]PATULJCI
    bzoj 1049 [HAOI2006]数字序列
    bzoj2091 [Poi2010]The Minima Game
    bzoj 1016 [JSOI2008]最小生成树计数
    bzoj5105 晨跑 数论lcm
    bzoj 1211 [HNOI2004]树的计数
    bzoj 1430 小猴打架 prufer 性质
    Swing界面组件的通用属性
    数字调节控件JSpinner的使用
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/5914473.html
Copyright © 2011-2022 走看看