Description
In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But several days later, his mother modified the rule so that he could get a candy only when the day of the month was a prime number and the month was also a prime number. He felt a bit upset because he could get fewer candies. What's worse, his mother changed the rule again and he had to answer a question before he could get a candy in those days. The question was that how many candies he could get in the given time interval. Hiqivenfin wanted to cry and asked you for help. He promised to give you half of a candy if you could help him to solve this problem.
Input
There are multiple test cases. The first line of input is an integer T (0 < T <= 50), indicating the number of test cases. Then T test cases follow. The i-th line of the next T lines contains two dates, the day interval of the question. The format of the date is "yyyy mm dd". You can assume both dates are valid. Hiqivenfin was born at 1000-01-01 and would not die after 2999-12-31, so the queries are all in this interval.
Hiqivenfin didn't seem to be an earthman, but the calendar was the same as that we usually use. That is to say, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.
Output
Output the number of candies Hiqivenfin could get in the time interval. Both sides of the interval are inclusive.
Sample Input
2 1000 01 01 1000 01 31 2000 02 01 2000 03 01
Sample Output
010
求给定时间范围内月和日都是质数的天数,直接模拟就好了。
#include<iostream> #include<algorithm> #include<cmath> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<vector> #include<functional> #include<stack> using namespace std; int n, a, b, c, d, e, f, sum; int r[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int check(int a, int b, int c, int d, int e, int f) { if (a < d) return 1; if (a > d) return -1; if (b < e) return 1; if (b > e) return -1; if (c < f) return 1; if (c > f) return -1; return 0; } int check(int x) { if (x == 2 || x == 3 || x == 5) return 1; if (x == 7 || x == 11 || x == 13) return 1; if (x == 17 || x == 19 || x == 23) return 1; if (x == 29 || x == 31) return 1; return 0; } int main(){ while (~scanf("%d", &n)) { while (n--) { cin >> a >> b >> c >> d >> e >> f; sum = 0; while (check(a, b, c, d, e, f) != 0) { if (a % 400 == 0 || (a % 100 != 0 && a % 4 == 0)) r[2] = 29; else r[2] = 28; if (check(b) && check(c)) sum++; c = c + 1; if (c > r[b]) c = 1, b = b + 1; if (b > 12) b = 1, a = a + 1; } if (check(b) && check(c)) sum++; cout << sum << endl; } } return 0; }
温故而知新#include<map> #include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define ms(x,y) memset(x,y,sizeof(x)) #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define loop(i,j,k) for (int i=j;i!=-1;i=k[i]) #define inone(x) scanf("%d",&x) #define intwo(x,y) scanf("%d%d",&x,&y) #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z) typedef long long LL; const int low(int x) { return x&-x; } const int mod = 1e9 + 7; const int N = 1e2 + 10; int T, f[N], g[4000][13][35]; int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int is(int x) { if (f[x] > -1) return f[x]; for (int i = 2; i < x; i++) if (x%i == 0) return f[x] = 0; return f[x] = 1; } struct date { int yy, mm, dd; date(int yy = 0, int mm = 0, int dd = 0) :yy(yy), mm(mm), dd(dd) {} bool operator <(const date&a)const { return yy == a.yy ? mm == a.mm ? dd < a.dd : mm < a.mm : yy < a.yy; } void read() { inthr(yy, mm, dd); } int ok() { return is(mm) && is(dd); } static int get(int yy, int mm) { if ((yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) && mm == 2) return 29; return m[mm]; } date next() { date d = *this; d.dd++; if (d.dd > get(d.yy, d.mm)) { d.dd = 1; d.mm++; if (d.mm > 12) d.mm = 1, d.yy++; } return d; } }a, b, c; void init() { ms(f, -1); f[1] = 0; c = date(1000, 1, 1); g[c.yy][c.mm][c.dd] = 0; for (int s = 0; c < date(2999, 12, 31);) { c = c.next(); s += c.ok(); g[c.yy][c.mm][c.dd] = s; } } int main() { init(); for (inone(T); T--;) { a.read(); b.read(); printf("%d\n", g[b.yy][b.mm][b.dd] - g[a.yy][a.mm][a.dd] + a.ok()); } return 0; }