洛谷P1328题解

题目

这题一直想写,但是鸽了好久。

大模拟。按题意模拟即可,没什么难度。

检查的时候看一下一批 a == i && b == j 中某个数在 i 和 j 中出现的个数。

因为我的数组是从 0 开始的,所以对于这个周期性重复来说好处理一些。

代码:

 

  1 #include<stdio.h>
  2 #define reg register
  3 #define ri reg int
  4 #define rep(i, x, y) for(ri i = x; i <= y; ++i)
  5 #define nrep(i, x, y) for(ri i = x; i >= y; --i)
  6 #define DEBUG 1
  7 #define ll long long
  8 #define il inline
  9 #define max(i, j) (i) > (j) ? (i) : (j)
 10 #define min(i, j) (i) < (j) ? (i) : (j)
 11 #define read(i) io.READ(i)
 12 #define print(i) io.WRITE(i)
 13 #define push(i) io.PUSH(i)
 14 struct IO {
 15     #define MAXSIZE (1 << 20)
 16     #define isdigit(x) (x >= '0' && x <= '9')
 17     char buf[MAXSIZE], *p1, *p2;
 18     char pbuf[MAXSIZE], *pp;
 19     #if DEBUG
 20     #else
 21         IO() : p1(buf), p2(buf), pp(pbuf) {}
 22         ~IO() {
 23             fwrite(pbuf, 1, pp - pbuf, stdout);
 24         }
 25     #endif
 26     inline char gc() {
 27         #if DEBUG
 28             return getchar();
 29         #endif
 30         if(p1 == p2)
 31             p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
 32         return p1 == p2 ? ' ' : *p1++;
 33     }
 34     inline bool blank(char ch) {
 35         return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
 36     }
 37     template <class T>
 38     inline void READ(T &x) {
 39         register double tmp = 1;
 40         register bool sign = 0;
 41         x = 0;
 42         register char ch = gc();
 43         for(; !isdigit(ch); ch = gc())
 44             if(ch == '-') sign = 1;
 45         for(; isdigit(ch); ch = gc())
 46             x = x * 10 + (ch - '0');
 47         if(ch == '.')
 48             for(ch = gc(); isdigit(ch); ch = gc())
 49                 tmp /= 10.0, x += tmp * (ch - '0');
 50         if(sign) x = -x;
 51     }
 52     inline void READ(char *s) {
 53         register char ch = gc();
 54         for(; blank(ch); ch = gc());
 55         for(; !blank(ch); ch = gc())
 56             *s++ = ch;
 57         *s = 0;
 58     }
 59     inline void READ(char &c) {
 60         for(c = gc(); blank(c); c = gc());
 61     }
 62     inline void PUSH(const char &c) {
 63         #if DEBUG
 64             putchar(c);
 65         #else
 66             if(pp - pbuf == MAXSIZE) {
 67                 fwrite(pbuf, 1, MAXSIZE, stdout);
 68                 pp = pbuf;
 69             }
 70             *pp++ = c;
 71         #endif
 72     }
 73     template <class T>
 74     inline void WRITE(T x) {
 75         if(x < 0) {
 76             x = -x;
 77             PUSH('-');
 78         }
 79         static T sta[35];
 80         T top = 0;
 81         do {
 82             sta[top++] = x % 10;
 83             x /= 10;
 84         }while(x);
 85         while(top)
 86             PUSH(sta[--top] + '0');
 87     }
 88     template <class T>
 89     inline void WRITE(T x, char lastChar) {
 90         WRITE(x);
 91         PUSH(lastChar);
 92     }
 93 } io;
 94 int n, na, nb, a[200], b[200];
 95 int check(int a, int b) {
 96     if(a == b) return 0;
 97     if(a == 0 && b == 2) return 1;
 98     if(a == 0 && b == 3) return 1;
 99     if(a == 1 && b == 0) return 1;
100     if(a == 1 && b == 3) return 1;
101     if(a == 2 && b == 1) return 1;
102     if(a == 2 && b == 4) return 1;
103     if(a == 3 && b == 2) return 1;
104     if(a == 3 && b == 4) return 1;
105     if(a == 4 && b == 0) return 1;
106     if(a == 4 && b == 1) return 1;
107     return -1;
108 }
109 int main() {
110     ri _a = 0, _b = 0;
111     read(n), read(na), read(nb);
112     rep(i, 0, na - 1) read(a[i]);
113     rep(i, 0, nb - 1) read(b[i]);
114     rep(i, 0, n - 1) {
115         if(check(a[i % na], b[i % nb]) == 1) ++_a;
116         else if(check(a[i % na], b[i % nb]) == -1) ++_b;
117     }
118     print(_a), push(' '), print(_b); 
119 }
View Code

 

posted @ 2021-02-20 15:35  1358id  阅读(27)  评论(0编辑  收藏  举报