找第一个大于n的整除k的数, 会发现, 他等于n 整除k + 1 在乘k。 输出即可。
从n+1年开始枚举, 并判断; 对于当前年i, 首先判断他的平闰年是否和n年相同。 然后对于第二个条件, 其实就是判断从n年到i年的天数是否整除7. 如果这两个条件都符合, 直接输出即可。
大佬直接维护天数%7变成每年的第一天是星期几,在算出n年的第一天是星期几, 并根据这个判断 具体算法见代码
#include <bits/stdc++.h> using namespace std; typedef long long LL; int isleap(int y) { if (y % 100 == 0) { return y % 400 == 0; } return y % 4 == 0; } int main() { int n; cin >> n; int w = 1; for (int i = 2016 ; i > 1000 ; -- i) { if (isleap(i)) { w -= 366 % 7; } else { w -= 365 % 7; } w += 7 , w %= 7; } for (int i = 1001 ; i <= n ; ++ i) { w += isleap(i) + 365; w %= 7; } int p = w; int q = isleap(n); int j = n + 1; while (1) { w += isleap(j) + 365; w %= 7; if (w == p && isleap(j) == q) { printf("%d\n" , j); break; } ++ j; } }如果他要让获得最大的巧克力数尽量多的话, 那么,应该让刷一个格子获得颜色多的尽量多刷, 所以 , 我们就可以尽量多刷获得巧克力多的, 然后,再加上获得巧克力少的 , 最后, 因为有重复 所以再减去他们的最小公倍数刷的格子获得的少的巧克力。
你也可以先算出刷红格子与蓝格子同时不算他们的最小公倍数的格子, 然后再加上他们最小公倍数的格子乘上刷红或蓝中获得巧克力的格子。
#include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { LL n , a , b , p , q; cin >> n >> a >> b >> p >> q; LL A = n / a , B = n / b , c = a / __gcd(a , b) * b; LL C = n / c; A -= C , B -= C; cout << A * p + B * q + max(p , q) * C << endl; }g ( 0 ) ( x ) = x g^{(0)}(x) = x g(0)(x)=x g ( 1 ) ( x ) = A x + B g^{(1)}(x) = Ax+B g(1)(x)=Ax+B g ( 2 ) ( x ) = A 2 x + A B + B g^{(2)}(x) = A^2x+AB+B g(2)(x)=A2x+AB+B g ( 3 ) ( x ) = A 3 x + A 2 B + A B + B g^{(3)}(x) = A^3x +A^2B+AB+B g(3)(x)=A3x+A2B+AB+B . . . . . . . . ........ ........ 由此可以推出: g ( n ) ( x ) = A n x + A n − 1 B + A n − 2 B + . . . . + A B + B g^{(n)}(x) = A^nx+A^{n-1}B+A^{n-2}B+....+AB+B g(n)(x)=Anx+An−1B+An−2B+....+AB+B 提公因式得: A n x + B × ( A n − 1 + A n − 2 + . . . . + A + 1 ) A^nx+B\times(A^{n-1}+A^{n-2}+....+A+1) Anx+B×(An−1+An−2+....+A+1) 根据等比数列公式得: A n x + A n − 1 A − 1 B = g ( n ) ( x ) A^nx+\frac{A^n-1}{A-1}B = g^{(n)}(x) Anx+A−1An−1B=g(n)(x) 求即可。 注意特判A == 1时, 答案等于 1 + B × n 1+B\times n 1+B×n
矩阵乘法优化dp 首先写出递推式 d p [ i ] = A × d p [ i − 1 ] + B dp[i] = A\times dp[i-1] + B dp[i]=A×dp[i−1]+B 然后构造矩阵方程: ( ) × ( d p [ i ] d p [ i − 1 ] ) = ( d p [ i + 1 ] d p [ i ] ) \begin{pmatrix}&\\&\end{pmatrix} \times \begin{pmatrix}dp[i]\\dp[i-1]\end{pmatrix}=\begin{pmatrix}dp[i+1]\\dp[i]\end{pmatrix} ()×(dp[i]dp[i−1])=(dp[i+1]dp[i])很容易得出里面的矩阵应为 ( A B 1 0 ) \begin{pmatrix}A&B\\1&0\end{pmatrix} (A1B0), 那么只要用矩阵快速幂将这个矩阵乘上n-1次并乘初始矩阵即可。
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int Q = 1e9 + 7; struct Matrix { int n , m , a[2][2]; Matrix (int _n = 0, int _m = 0) { n = _n , m = _m; memset(a , 0 , sizeof(a)); } Matrix operator * (const Matrix &R) const { Matrix res(n , R.m); for (int i = 0 ; i < n ; ++ i) { for (int j = 0 ; j < m ; ++ j) { for (int k = 0 ; k < R.m ; ++ k) { res.a[i][k] += (LL)a[i][j] * R.a[j][k] % Q; res.a[i][k] %= Q; } } } return res; } }; int main() { LL A , B , n , x; cin >> A >> B >> n >> x; Matrix I(1 , 2); I.a[0][0] = x , I.a[0][1] = 1; Matrix P(2 , 2); P.a[0][0] = A; P.a[1][0] = B; P.a[1][1] = 1; while (n) { if (n & 1) { I = I * P; } P = P * P; n >>= 1; } cout << I.a[0][0] << endl; }设f[s]表示当前状态下获胜的最大概率 状压, s中每一位表示每个骑士, 0表示被淘汰, 1表示存在。 转移方程为。 f [ s ] = m a x s i z e ( f [ s ⊕ ( 1 < < j ) ] × a [ i ] [ j ] + f [ s ⊕ ( 1 < < j ) × a [ j ] [ i ] ) f[s] = maxsize({f[s \oplus (1<<j)] \times a[i][j] + f[s\oplus(1<<j)}\times a[j][i]) f[s]=maxsize(f[s⊕(1<<j)]×a[i][j]+f[s⊕(1<<j)×a[j][i])
