• 2019杭电多校第五场


    比赛记录

    @byf水题写错一发,边界没考虑,变量还写错,一发罚时加很晚才过。1004忘记结构体重载运算符中的gcd也带一个log,TLE2发,而且这题花了太多时间。

    @shl exkmp板子想不起来,先用后缀数组T了一发

    @lfw 一上场用kmp写exkmp的题,结果发现不太对,有点冲动了,应该能推出样例再写的。1005一开始没想清楚,计数有问题,所以花了很久时间用对样例的调试来想清楚怎么计数。

    题解

    1001 fraction
    题解:https://blog.csdn.net/baiyifeifei/article/details/98606715

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    LL p,a;
    //solve "pa/pb<x/y<=qa/qb" 's min(y),min(x)
    void solve(LL pa,LL pb,LL qa,LL qb,LL &x,LL &y)
    {
        LL z=(pa+pb-1)/pb;
        if(z<=qa/qb){
            x=z;y=1;
            return ;
        }
        pa-=(z-1)*pb;qa-=(z-1)*qb;
        solve(qb,qa,pb,pa,y,x);
        x+=(z-1)*y;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        LL x,y;
        while(t--)
        {
            scanf("%lld%lld",&p,&a);
            solve(p,a,p,a-1,x,y);
            printf("%lld/%lld
    ",x*a-p*y,x);
        }
    }
    View Code

    1002 three arrays

    题解:https://blog.csdn.net/liufengwei1/article/details/98536161

      1 #include<bits/stdc++.h>
      2 #define maxl 100010
      3 using namespace std;
      4  
      5 int n,m,tota,totb,sum;
      6 int ans[maxl],mi[30];
      7 int a[maxl],b[maxl];
      8 int suma[maxl*31],sumb[maxl*31];
      9 int tra[maxl*31][2],trb[maxl*31][2];
     10 int q[maxl],w[maxl];
     11 vector <int> numa[maxl*31],numb[maxl*31];
     12 vector <int> :: iterator it;
     13 bool ina[maxl],inb[maxl];
     14  
     15 inline void inserta(int id,int x)
     16 {
     17     int u=0,c;
     18     for(int i=29;i>=0;i--)
     19     {
     20         suma[u]++;
     21         c=(x>>i)&1;
     22         if(!tra[u][c])
     23             tra[u][c]=++tota;
     24         u=tra[u][c];
     25     }
     26     suma[u]++;numa[u].push_back(id);
     27 }
     28  
     29 inline void insertb(int id,int x)
     30 {
     31     int u=0,c;
     32     for(int i=29;i>=0;i--)
     33     {
     34         sumb[u]++;
     35         c=(x>>i)&1;
     36         if(!trb[u][c])
     37             trb[u][c]=++totb;
     38         u=trb[u][c];
     39     }
     40     sumb[u]++;numb[u].push_back(id);
     41 }
     42  
     43 inline void prework()
     44 {
     45     for(int i=0;i<=tota;i++)
     46     {
     47         tra[i][0]=tra[i][1]=0;
     48         suma[i]=0;numa[i].clear();    
     49     }
     50     for(int i=0;i<=totb;i++)
     51     {
     52         trb[i][0]=trb[i][1]=0;
     53         sumb[i]=0,numb[i].clear();
     54     }
     55     scanf("%d",&n);
     56     tota=0;totb=0;
     57     for(int i=1;i<=n;i++)
     58     {    
     59         scanf("%d",&a[i]);
     60         inserta(i,a[i]);ina[i]=true;
     61     }
     62     for(int i=1;i<=n;i++)
     63     {
     64         scanf("%d",&b[i]);
     65         insertb(i,b[i]);inb[i]=true;
     66     }
     67 }
     68  
     69 inline int finda(int x)
     70 {
     71     int u=0,c,tmp=0;
     72     for(int i=29;i>=0;i--)
     73     {
     74         c=(x>>i)&1;
     75         if(!tra[u][c])
     76             u=tra[u][c^1];
     77         else 
     78             u=tra[u][c];
     79     }
     80     it=numa[u].end();--it;
     81     return (*it);
     82 }
     83  
     84 inline int findb(int x)
     85 {
     86     int u=0,c;
     87     for(int i=29;i>=0;i--)
     88     {
     89         c=(x>>i)&1;
     90         if(!trb[u][c])
     91             u=trb[u][c^1];
     92         else
     93             u=trb[u][c];
     94     }
     95     it=numb[u].end();--it;
     96     return (*it);
     97 }
     98  
     99 inline void dela(int x)
    100 {
    101     int u=0,c,last;suma[u]--;
    102     for(int i=29;i>=0;i--)
    103     {
    104         c=(x>>i)&1;last=u;
    105         u=tra[u][c];
    106         suma[u]--;
    107         if(suma[u]==0)
    108             tra[last][c]=0;
    109     }
    110     it=numa[u].end();--it;
    111     numa[u].erase(it);
    112 }
    113  
    114 inline void delb(int x)
    115 {
    116     int u=0,c,last;sumb[u]--;
    117     for(int i=29;i>=0;i--)
    118     {
    119         c=(x>>i)&1;last=u;
    120         u=trb[u][c];
    121         sumb[u]--;
    122         if(sumb[u]==0)
    123             trb[last][c]=0; 
    124     }
    125     it=numb[u].end();--it;
    126     numb[u].erase(it);
    127 }
    128  
    129 inline void dfs(int k)
    130 {
    131     int id;
    132     if(k&1)
    133     {
    134         id=finda(b[q[k-1]]);
    135         if(id==q[k-2] && k-2>0)
    136         {
    137             delb(b[q[k-1]]);
    138             dela(a[id]);
    139             inb[q[k-1]]=false;
    140             ina[id]=false;
    141             ans[++ans[0]]=b[q[k-1]]^a[id];
    142             k-=2;sum--;
    143         }
    144         else
    145         {
    146             w[k]=b[q[k-1]]^a[id];
    147             q[k]=id;k++;
    148         }
    149         if(sum>0)
    150             dfs(k);
    151     }
    152     else
    153     {
    154         id=findb(a[q[k-1]]);
    155         if(id==q[k-2] && k-2>0)
    156         {
    157             dela(a[q[k-1]]);
    158             delb(b[id]);
    159             ina[q[k-1]]=false;
    160             inb[id]=false;
    161             ans[++ans[0]]=a[q[k-1]]^b[id];
    162             k-=2;sum--;
    163         }
    164         else
    165         {
    166             w[k]=a[q[k-1]]^b[id];
    167             q[k]=id;k++;
    168         }
    169         if(sum>0)
    170             dfs(k);
    171     }
    172 }
    173  
    174 inline void mainwork()
    175 {
    176     ans[0]=0;sum=n;
    177     for(int i=1;i<=n;i++)
    178     if(ina[i])
    179     {
    180         q[1]=i;
    181         dfs(2);
    182     }
    183 }
    184  
    185 inline void print()
    186 {
    187     sort(ans+1,ans+1+ans[0]);
    188     for(int i=1;i<=n;i++)
    189         printf("%d%c",ans[i],(i==n)?'
    ':' ');
    190 }
    191  
    192 int main()
    193 {
    194     mi[0]=1;
    195     for(int i=1;i<=29;i++)
    196         mi[i]=mi[i-1]*2;
    197     int t;
    198     scanf("%d",&t);
    199     for(int i=1;i<=t;i++)
    200     {
    201         prework();
    202         mainwork();
    203         print();
    204     }
    205     return 0;
    206 }
    View Code

    1003 geometric problem

    unsolved

    1004 equation

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 
      4 namespace IO{ 
      5     #define BUF_SIZE 100000 
      6     #define OUT_SIZE 100000 
      7     #define ll long long 
      8 
      9     bool IOerror=0; 
     10     inline char nc(){ 
     11         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; 
     12         if (p1==pend){ 
     13             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); 
     14             if (pend==p1){IOerror=1;return -1;} 
     15         } 
     16         return *p1++; 
     17     } 
     18     inline bool blank(char ch){return ch==' '||ch=='
    '||ch=='
    '||ch=='	';} 
     19     inline bool read(int &x){ 
     20         bool sign=0; char ch=nc(); x=0; 
     21         for (;blank(ch);ch=nc()); 
     22         if (IOerror) return false; 
     23         if (ch=='-')sign=1,ch=nc(); 
     24         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; 
     25         if (sign)x=-x; return true;
     26     } 
     27     inline bool read(ll &x){ 
     28         bool sign=0; char ch=nc(); x=0; 
     29         for (;blank(ch);ch=nc()); 
     30         if (IOerror) return false; 
     31         if (ch=='-')sign=1,ch=nc(); 
     32         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; 
     33         if (sign)x=-x; return true;
     34     } 
     35     inline bool read(double &x){ 
     36         bool sign=0; char ch=nc(); x=0; 
     37         for (;blank(ch);ch=nc()); 
     38         if (IOerror) return false; 
     39         if (ch=='-')sign=1,ch=nc(); 
     40         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; 
     41         if (ch=='.'){ 
     42             double tmp=1; ch=nc(); 
     43             for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0'); 
     44         } 
     45         if (sign)x=-x; return true;
     46     } 
     47     inline bool read(char *s){ 
     48         char ch=nc(); 
     49         for (;blank(ch);ch=nc()); 
     50         if (IOerror) return false; 
     51         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; 
     52         *s=0; return true;
     53     } 
     54     inline void read(char &c){ 
     55         for (c=nc();blank(c);c=nc()); 
     56         if (IOerror){c=-1;return;} 
     57     } 
     58     //fwrite->write 
     59     struct Ostream_fwrite{ 
     60         char *buf,*p1,*pend; 
     61         Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;} 
     62         void out(char ch){ 
     63             if (p1==pend){ 
     64                 fwrite(buf,1,BUF_SIZE,stdout);p1=buf; 
     65             } 
     66             *p1++=ch; 
     67         } 
     68         void print(int x){ 
     69             static char s[15],*s1;s1=s; 
     70             if (!x)*s1++='0';if (x<0)out('-'),x=-x; 
     71             while(x)*s1++=x%10+'0',x/=10; 
     72             while(s1--!=s)out(*s1); 
     73         } 
     74         void println(int x){ 
     75             static char s[15],*s1;s1=s; 
     76             if (!x)*s1++='0';if (x<0)out('-'),x=-x; 
     77             while(x)*s1++=x%10+'0',x/=10; 
     78             while(s1--!=s)out(*s1); out('
    '); 
     79         } 
     80         void print(ll x){ 
     81             static char s[25],*s1;s1=s; 
     82             if (!x)*s1++='0';if (x<0)out('-'),x=-x; 
     83             while(x)*s1++=x%10+'0',x/=10; 
     84             while(s1--!=s)out(*s1); 
     85         } 
     86         void println(ll x){ 
     87             static char s[25],*s1;s1=s; 
     88             if (!x)*s1++='0';if (x<0)out('-'),x=-x; 
     89             while(x)*s1++=x%10+'0',x/=10; 
     90             while(s1--!=s)out(*s1); out('
    '); 
     91         } 
     92         void print(double x,int y){ 
     93             static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000, 
     94                 1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL, 
     95                 100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL}; 
     96             if (x<-1e-12)out('-'),x=-x;x*=mul[y]; 
     97             ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1; 
     98             ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2); 
     99             if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i) {}; print(x3);} 
    100         } 
    101         void println(double x,int y){print(x,y);out('
    ');} 
    102         void print(char *s){while (*s)out(*s++);} 
    103         void println(char *s){while (*s)out(*s++);out('
    ');} 
    104         void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}} 
    105         ~Ostream_fwrite(){flush();} 
    106     }Ostream; 
    107     inline void print(int x){Ostream.print(x);} 
    108     inline void println(int x){Ostream.println(x);} 
    109     inline void print(char x){Ostream.out(x);} 
    110     inline void println(char x){Ostream.out(x);Ostream.out('
    ');} 
    111     inline void print(ll x){Ostream.print(x);} 
    112     inline void println(ll x){Ostream.println(x);} 
    113     inline void print(double x,int y){Ostream.print(x,y);} 
    114     inline void println(double x,int y){Ostream.println(x,y);} 
    115     inline void print(char *s){Ostream.print(s);} 
    116     inline void println(char *s){Ostream.println(s);} 
    117     inline void println(){Ostream.out('
    ');} 
    118     inline void flush(){Ostream.flush();}
    119     #undef ll 
    120     #undef OUT_SIZE 
    121     #undef BUF_SIZE 
    122 };
    123 using namespace IO;
    124 
    125 
    126 typedef long long LL;
    127 const int size=1e5+5;
    128 int suma[size],sumb[size];
    129 struct frac{
    130     int son,mon;
    131     frac(){}
    132     frac(int _son,int _mon)
    133     {
    134         if(_son==0) {son=0,mon=1;}
    135         else {
    136             int g=__gcd(_son,_mon);
    137             son=_son/g;
    138             mon=_mon/g;
    139             if(son>0&&mon<0)son=-son,mon=-mon;
    140         }
    141     }
    142     friend bool operator<(frac x,frac y)
    143     {
    144         return 1LL*y.mon*x.son<1LL*x.mon*y.son;
    145     }
    146     friend bool operator==(frac x,frac y)
    147     {
    148         if((x.son==y.son)&&(x.mon==y.mon)) return true;;
    149         return false;
    150     }
    151     friend bool operator<=(frac x,frac y)
    152     {
    153         if(x<y) return true;
    154         if((x.son==y.son)&&(x.mon==y.mon)) return true;
    155         return false;
    156     }
    157 };
    158 struct line{
    159     int a,b;
    160     frac p;
    161     line(){}
    162     line(int a,int b):a(a),b(b),p(-b,a){}
    163     friend bool operator<(line x,line y)
    164     {
    165         return x.p<y.p;
    166     }
    167 }L[size];
    168 vector<frac> ans;
    169 int main()
    170 {
    171     int t;
    172     //scanf("%d",&t);
    173     read(t); 
    174     int n,c;
    175     while(t--)
    176     {
    177         //scanf("%d%d",&n,&c);
    178         read(n);read(c);
    179         suma[0]=sumb[0]=0;
    180         bool flag;
    181         int a,b;
    182         for(int i=1;i<=n;i++)
    183         {
    184             //scanf("%d%d",&a,&b);
    185             read(a);read(b);
    186             L[i]=line(a,b);
    187         }
    188         sort(L+1,L+1+n);
    189         ans.clear();
    190         for(int i=1;i<=n;i++)
    191         {
    192             suma[i]=suma[i-1]+L[i].a;
    193             sumb[i]=sumb[i-1]+L[i].b;
    194         }
    195         int newa=-suma[n],newb=-sumb[n];
    196         frac tp;
    197         if(newa==0)
    198         {
    199             if(newb==c)
    200             {
    201                 println(-1);
    202                 //puts("-1");
    203                 continue;
    204             }
    205         }
    206         if(newa!=0){
    207             tp=frac(c-newb,newa);
    208             if(tp<=L[1].p) ans.push_back(tp);
    209         }
    210         flag=true;
    211         for(int i=1;i<n;i++)
    212         {
    213             newa=2*suma[i]-suma[n];
    214             newb=2*sumb[i]-sumb[n];
    215             if(newa==0)
    216             {
    217                 if(newb==c)
    218                 {
    219                     println(-1);
    220                     //puts("-1");
    221                     flag=false;
    222                     break;
    223                 }
    224                 else continue;
    225             }
    226             tp=frac(c-newb,newa);
    227             if(tp<=L[i+1].p&&L[i].p<=tp) ans.push_back(tp);
    228         }
    229         if(!flag) continue;
    230         newa=suma[n],newb=sumb[n];
    231         if(newa==0)
    232         {
    233             if(newb==c)
    234             {
    235                 println(-1);
    236                 //puts("-1");
    237                 continue;
    238             }
    239         }
    240         if(newa!=0){
    241             tp=frac(c-newb,newa);
    242             if(L[n].p<=tp) ans.push_back(tp);
    243         }
    244         sort(ans.begin(),ans.end());
    245         ans.erase(unique(ans.begin(),ans.end()),ans.end());
    246         int len=ans.size();
    247         //printf("%d",len);
    248         print(len);
    249         for(int i=0;i<len;i++)
    250         {
    251             print(' ');
    252             print(ans[i].son);
    253             print('/');
    254             print(ans[i].mon);
    255             //printf(" %d/%d",ans[i].son,ans[i].mon);
    256         }
    257         print('
    ');
    258         //puts("");
    259     }
    260     return 0;
    261 }
    View Code

    1005 permutation 1

    题解:https://blog.csdn.net/liufengwei1/article/details/98515203

      1 #include<bits/stdc++.h>
      2 #define maxl 45
      3 using namespace std; 
      4  
      5 int n;
      6 long long k;
      7 int p[maxl];
      8 int ans[maxl];
      9 long long jc[maxl];
     10 int ok[maxl*2];
     11 struct node
     12 {
     13     int a[maxl];
     14     int in[maxl];
     15     void init()
     16     {
     17         for(int i=0;i<=20;i++)
     18             a[i]=0,in[i]=false;
     19     }
     20 };
     21 vector <node> tmp;
     22 vector <node> :: iterator it;
     23  
     24 inline void prework()
     25 {
     26     scanf("%d%lld",&n,&k);
     27 }
     28  
     29 inline void solve(int u)
     30 {
     31     for(int i=1-n;i<=n-1;i++)
     32         ok[i+n]=0;
     33     bool flag;node d;
     34     d.init();
     35     if(u==1)
     36     {
     37         tmp.clear();
     38         if(p[u]<0)
     39         {
     40             for(int i=n;i>=1-p[u];i--)
     41             {
     42                 d.a[1]=i;d.a[2]=i+p[u];
     43                 d.in[i]=true;d.in[i+p[u]]=true;
     44                 for(int j=1;j<=n;j++)
     45                 if(!d.in[j])
     46                     ok[j-d.a[2]+n]++;
     47                 tmp.push_back(d);
     48                 d.in[i]=false;d.in[i+p[u]]=false;
     49             }
     50         }
     51         else
     52         {
     53             for(int i=1;i<=n-p[u];i++)
     54             {
     55                 d.a[1]=i;d.a[2]=i+p[u];
     56                 d.in[i]=true;d.in[i+p[u]]=true;
     57                 for(int j=1;j<=n;j++)
     58                 if(!d.in[j])
     59                     ok[j-d.a[2]+n]++;
     60                 tmp.push_back(d);
     61                 d.in[i]=false;d.in[i+p[u]]=false;
     62             }
     63         }
     64     }
     65     else
     66     {
     67         int x;
     68         for(it=tmp.begin();it!=tmp.end();)
     69         {
     70             x=it->a[u]+p[u];
     71             if(x<=n && x>0)
     72             {    if( !(it->in[x]))
     73                 {
     74                     it->a[u+1]=x;
     75                     it->in[x]=true;
     76                     for(int j=1;j<=n;j++)
     77                     if(!(it->in[j]))
     78                         ok[j-x+n]++;
     79                     ++it;
     80                 }
     81                 else
     82                     tmp.erase(it);
     83             }
     84             else
     85                 tmp.erase(it);
     86         }
     87     }
     88 }
     89  
     90 inline int abs(int x)
     91 {
     92     if(x<0) return -x;
     93     else return x;
     94 }
     95  
     96 inline void dfs(int u,long long res)
     97 {
     98     long long t;
     99     int sum;
    100     for(int i=1-n;i<=n-1;i++)
    101     if(ok[i+n])
    102     {
    103         sum=ok[i+n];
    104         if(k<=sum*jc[n-1-u])
    105         {
    106             p[u]=i;
    107             solve(u);
    108             if(u<n-1)
    109                 dfs(u+1,k);    
    110             return;
    111         }
    112         k-=sum*jc[n-1-u];
    113     }
    114 }
    115  
    116 inline void mainwork()
    117 {
    118     for(int i=1-n;i<=n-1;i++)
    119         ok[i+n]=n-abs(i);
    120     ok[0+n]=0;
    121     tmp.clear();
    122     dfs(1,k);
    123 }
    124  
    125 inline void print()
    126 {
    127     it=tmp.begin();
    128     for(int i=1;i<=n;i++)
    129         printf("%d%c",it->a[i],(i==n)?'
    ':' ');
    130 }
    131  
    132 int main()
    133 {
    134     jc[0]=1;jc[1]=1;
    135     for(int i=1;i<=20;i++)
    136         jc[i]=jc[i-1]*i;
    137     int t;
    138     scanf("%d",&t);
    139     for(int i=1;i<=t;i++)
    140     {
    141         prework();
    142         mainwork();
    143         print();
    144     }
    145     return 0;
    146 }
    View Code

    1006 string matching

     1 #include<bits/stdc++.h>
     2 using namespace std; 
     3 typedef long long ll;
     4 using namespace std;
     5 typedef long long ll;
     6 const int maxn = 2e6 + 5; 
     7 char ss[maxn];
     8 ll Next[maxn], extend[maxn];
     9 #define fi first
    10 #define se second
    11 #define INF 0x3f3f3f3f
    12 
    13 //预处理计算Next数组
    14 void getNext(char str[])
    15 {
    16     int i = 0, j, po, len = strlen(str);
    17     Next[0] = len; //初始化next[0]
    18     while (str[i] == str[i + 1] && i + 1 < len)
    19         i++;
    20     Next[1] = i; //计算next[1]
    21     po = 1; //初始化po的位置
    22     for (i = 2; i < len; i++) {
    23         if (Next[i - po] + i < Next[po] + po) //第一种情况,可以直接得到next[i]的值
    24             Next[i] = Next[i - po];
    25         else //第二种情况,要继续匹配才能得到next[i]的值
    26         {
    27             j = Next[po] + po - i;
    28             if (j < 0)
    29                 j = 0; //如果i>po+Next[po],则要从头开始匹配
    30             while (i + j < len && str[j] == str[j + i])
    31                 j++;
    32             Next[i] = j;
    33             po = i; //更新po的位置
    34         }
    35     }
    36 }
    37 
    38 //计算extend数组
    39 void EXKMP(char s1[], char s2[])
    40 {
    41     int i = 0, j, po, len = strlen(s1), l2 = strlen(s2);
    42     getNext(s2); 
    43     while (s1[i] == s2[i] && i < l2 && i < len)
    44         i++;
    45     extend[0] = i;
    46     po = 0; 
    47     for (i = 1; i < len; i++) {
    48         if (Next[i - po] + i < extend[po] + po) 
    49             extend[i] = Next[i - po];
    50         else 
    51         {
    52             j = extend[po] + po - i;
    53             if (j < 0)
    54                 j = 0; 
    55             while (i + j < len && j < l2 && s1[j + i] == s2[j])
    56                 j++;
    57             extend[i] = j;
    58             po = i; 
    59         }
    60     }
    61 }
    62 
    63 int t;
    64 int main()
    65 {
    66     scanf("%d", &t);
    67     while (t--) 
    68     {
    69         scanf("%s", ss);
    70         EXKMP(ss, ss);
    71         int len = strlen(ss);
    72         ll ans = 0;
    73         for(int i=1;i<len;++i)
    74         {
    75             if(extend[i] + i == len)
    76                 ans += extend[i];
    77             else
    78                 ans += extend[i] + 1;
    79         }
    80         printf("%lld
    ", ans);
    81     }
    82     return 0;
    83 }
    View Code

    1007 permutation 2

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int mod = 998244353;
     4 const int size=1e5+5;
     5 int dp[size];
     6 void init()
     7 {
     8     dp[0]=1;dp[1]=1;dp[2]=1;dp[3]=2;
     9     for(int i=4;i<size;i++)
    10     {
    11         dp[i]=(dp[i-1]+dp[i-3])%mod;
    12     }
    13 }
    14 int main()
    15 {
    16     int t;
    17     int n,x,y;
    18     scanf("%d",&t);
    19     init();
    20     while(t--)
    21     {
    22         scanf("%d%d%d",&n,&x,&y);
    23         int beg,ed;
    24         if(x==1) beg=1;else beg=x+1;
    25         if(y==n) ed=n;else ed=y-1;
    26         if(ed-beg<0)
    27         {
    28             puts("0");
    29             continue;
    30         }
    31         printf("%d
    ",dp[ed-beg]);
    32     }
    33 }
    View Code

    1008 line symmetric

    题解:https://blog.csdn.net/liufengwei1/article/details/98664030

      1 #include<bits/stdc++.h>
      2 #define maxl 1010
      3 #define eps 1e-9
      4 using namespace std;
      5  
      6 inline int sgn(double x)
      7 {
      8     if(x>-eps && x<eps) return 0;
      9     if(x<0) return -1;
     10     else    return 1;
     11 }
     12  
     13 struct point
     14 {
     15     double x,y;
     16     point(double a=0,double b=0)
     17     {
     18         x=a,y=b;
     19     }
     20     point operator - (const point &b)const
     21     {
     22         return point(x-b.x,y-b.y);
     23     }
     24     point operator + (const point &b)const
     25     {
     26         return point(x+b.x,y+b.y);
     27     }
     28     friend point operator / (point p,double t)
     29     {
     30         return point(p.x/t,p.y/t);
     31     }
     32 };
     33 inline double dot(point a,point b)
     34 {
     35     return a.x*b.x+a.y*b.y;
     36 }
     37 inline double det(point a,point b)
     38 {
     39     return a.x*b.y-a.y*b.x;
     40 }
     41 struct line
     42 {
     43     point s,e;double k;
     44     line(point a=point(),point b=point())
     45     {
     46         s=a,e=b;
     47         k=atan2(e.y-s.y,e.x-s.x);
     48     }
     49     bool operator < (const line &b)const
     50     {
     51         return k<b.k;
     52     }
     53 };
     54  
     55 int n;
     56 point a[maxl]; 
     57 line b[maxl];
     58 bool ans,flag;
     59  
     60 inline void prework()
     61 {
     62     scanf("%d",&n);
     63     for(int i=1;i<=n;i++)
     64         scanf("%lf%lf",&a[i].x,&a[i].y);
     65 }
     66  
     67 inline void mainwork()
     68 {
     69     if(n<=4)
     70     {
     71         ans=true;
     72         return;
     73     }
     74     /*if(n==6 && a[1].x==0 && a[1].y==12)
     75     {
     76         ans=false;
     77         return;
     78     }*/
     79     ans=false;point mid,midd;
     80     int s,t,ss,tt,cnt,lasts,lastt;
     81     bool lastflag; 
     82     for(int i=1;i<=n;i++)
     83     {
     84         s=i-1;t=i+1;
     85         if(s<1) s=n;
     86         if(t>n) t=1;
     87         mid=a[s]+a[t];mid=mid/2;
     88         ss=s;tt=t;cnt=0;lastflag=false;
     89         if(sgn(dot(a[i]-mid,a[t]-a[s]))!=0)
     90             cnt=1;
     91         for(int j=1;j<=n/2-1;j++)
     92         {         
     93              
     94             lasts=ss;lastt=tt;
     95             ss--;tt--;
     96             if(ss<1) ss=n;
     97             if(tt>n) tt=1;
     98             midd=a[ss]+a[tt];midd=midd/2;
     99             if(lastflag)
    100             {
    101                 if(sgn(dot(a[ss]-mid,a[s]-mid))<=0 && 
    102                    sgn(dot(a[lasts]-mid,a[s]-mid))<=0)
    103                 {
    104                     cnt++;
    105                     break;
    106                 }
    107                 if(sgn(dot(a[tt]-mid,a[t]-mid))<=0 && 
    108                    sgn(dot(a[lastt]-mid,a[t]-mid))<=0)
    109                 {
    110                     cnt++;
    111                     break;
    112                 }
    113             }
    114             lastflag=false;  
    115             if(sgn(dot(a[t]-a[s],mid-midd))!=0 ||
    116                sgn(dot(a[tt]-a[ss],mid-midd))!=0)
    117                    cnt++,lastflag=true; 
    118  
    119             if(sgn(dot(a[ss]-mid,a[lasts]-mid))<=0 &&
    120                sgn(dot(a[tt]-mid,a[lastt]-mid))<=0)
    121                    cnt++,lastflag=true;
    122         }
    123         if(cnt<=1)
    124         {
    125             ans=true;
    126             return;
    127         }
    128     }
    129     for(int i=1;i<=n && !ans;i++)
    130     {
    131         s=i;t=i+1;
    132         if(s<1) s=n;
    133         if(t>n) t=1;
    134         ss=s;tt=t;lastflag=false;
    135         mid=a[s]+a[t];mid=mid/2;cnt=0;
    136         for(int j=1;j<=(n-1)/2;j++)
    137         {      
    138                
    139             lasts=ss;lastt=tt;      
    140             ss--;tt++;
    141             if(ss<1) ss=n;
    142             if(tt>n) tt=1;
    143             if(lastflag)
    144             {
    145                 if(sgn(dot(a[ss]-mid,a[s]-mid))<=0 && 
    146                    sgn(dot(a[lasts]-mid,a[s]-mid))<=0)
    147                 {
    148                     cnt++;
    149                     break;
    150                 }
    151                 if(sgn(dot(a[tt]-mid,a[t]-mid))<=0 && 
    152                    sgn(dot(a[lastt]-mid,a[t]-mid))<=0)
    153                 {
    154                     cnt++;
    155                     break;
    156                 }
    157             }
    158             lastflag=false;
    159             midd=a[ss]+a[tt];midd=midd/2;
    160             if(sgn(dot(a[t]-a[s],mid-midd))!=0 ||
    161                sgn(dot(a[tt]-a[ss],mid-midd))!=0)
    162                    cnt++,lastflag=true; 
    163  
    164             if(sgn(dot(a[ss]-mid,a[lasts]-mid))<=0 &&
    165                sgn(dot(a[tt]-mid,a[lastt]-mid))<=0)
    166                    cnt++,lastflag=true;
    167         }
    168           if(cnt<=1)
    169           {
    170             ans=true;
    171             return;
    172         }
    173     }
    174 }
    175  
    176 inline void print()
    177 {
    178     if(ans)
    179         puts("Y");
    180     else
    181         puts("N"); 
    182 }
    183  
    184 int main()
    185 {
    186     int t;
    187     scanf("%d",&t);
    188     for(int i=1;i<=t;i++)
    189     {
    190         prework();
    191         mainwork();
    192         print();
    193     }
    194     return 0;
    195 }
    View Code

    1009 discrete logarithm problem

    unsolved

    1010 find hidden array

    unsolved

  • 相关阅读:
    设计模式(十):Decorator装饰者模式 -- 结构型模式
    设计模式(九):Composite组合模式 -- 结构型模式
    Anagrams
    Gas Station
    Unique Binary Search Trees II
    Trapping Rain Water
    4Sum
    3Sum Closest
    3Sum
    Longest Valid Parentheses
  • 原文地址:https://www.cnblogs.com/csushl/p/11306526.html
Copyright © 2020-2023  润新知