3 条题解

  • 4
    @ 2026-9-28 13:04:30

    做法是非常简单的,考虑到

    ai=2bi−1+1a_i=2b_{i-1}+1

    而

    bi=2bi−1+ai−1+2,b_i=2b_{i-1}+a_{i-1}+2,

    考虑直接建立矩阵快速幂:

    考虑如下答案矩阵:

    [aibi1]\begin{bmatrix} a_i\\ b_i\\ 1 \end{bmatrix}

    其中

    ai=2bi−1+1a_i=2b_{i-1}+1 bi=ai−1+2bi−1+2b_i=a_{i-1}+2b_{i-1}+2 1=1(我去不早说)1=1(我去不早说)

    可以推出递推矩阵如下:

    $$\begin{bmatrix} 0&2&1\\ 1&2&2\\ 0&0&1 \end{bmatrix} \begin{bmatrix} a_{i-1}\\ b_{i-1}\\ 1 \end{bmatrix}= \begin{bmatrix} a_i\\ b_i\\ 1 \end{bmatrix}. $$

    所以

    $${\begin{bmatrix} 0&2&1\\ 1&2&2\\ 0&0&1 \end{bmatrix}}^{n-1} \begin{bmatrix} 1\\ 2\\ 1 \end{bmatrix}= \begin{bmatrix} a_n\\ b_n\\ 1 \end{bmatrix}. $$

    an,bna_n,b_n 即为答案。

    此时复杂度为 O(TN3log⁡n)O(TN^3\log n),其中 N=3N=3 为矩阵大小。此时运算量约为 1.4×1091.4\times 10^9,无法通过。

    考虑常见优化:
    1.光速幂,预处理递推矩阵 AA 的幂 A0,A1,...,AB−1A^0,A^1,...,A^{B-1} 和 AB,A2B,...,AB2A^B,A^{2B},...,A^{B^2}。
    取 B=1048576B=1048576,复杂度 O((B+T)N3)O((B+T)N^3)。
    2.循环展开,一维数组。3*3 的矩阵乘法可以手动拆开。
    3.使用 unsigned long long 可以减小常数。

    参考实现:

    #include<bits/stdc++.h>
    using namespace std;
    #define int unsigned long long
    const int N=(1<<20),p=998244353;
    struct mat{int a[9];}A[N],B[N],I;
    inline mat mul(mat a,mat b){mat c;c.a[0]=(a.a[0]*b.a[0]+a.a[1]*b.a[3]+a.a[2]*b.a[6])%p;c.a[1]=(a.a[0]*b.a[1]+a.a[1]*b.a[4]+a.a[2]*b.a[7])%p;c.a[2]=(a.a[0]*b.a[2]+a.a[1]*b.a[5]+a.a[2]*b.a[8])%p;c.a[3]=(a.a[3]*b.a[0]+a.a[4]*b.a[3]+a.a[5]*b.a[6])%p;c.a[4]=(a.a[3]*b.a[1]+a.a[4]*b.a[4]+a.a[5]*b.a[7])%p;c.a[5]=(a.a[3]*b.a[2]+a.a[4]*b.a[5]+a.a[5]*b.a[8])%p;c.a[6]=(a.a[6]*b.a[0]+a.a[7]*b.a[3]+a.a[8]*b.a[6])%p;c.a[7]=(a.a[6]*b.a[1]+a.a[7]*b.a[4]+a.a[8]*b.a[7])%p;c.a[8]=(a.a[6]*b.a[2]+a.a[7]*b.a[5]+a.a[8]*b.a[8])%p;return c;}
    void init()
    	{
    	memset(I.a,0,sizeof I.a);I.a[0]=I.a[4]=I.a[8]=1;
    	A[0]=B[0]=I;
    	A[1].a[0]=A[1].a[6]=A[1].a[7]=0;
    	A[1].a[2]=A[1].a[3]=A[1].a[8]=1;
    	A[1].a[1]=A[1].a[4]=A[1].a[5]=2;
    	for(int i=2;i<N;i++)A[i]=mul(A[i-1],A[1]);
    	B[1]=mul(A[N-1],A[1]);
    	for(int i=2;i<N;i++)B[i]=mul(B[i-1],B[1]);
    	}
    int ans1=0,ans2=0;
    void sol()
    	{
    	mat ans=I;ans.a[4]=ans.a[8]=0;ans.a[3]=2;ans.a[6]=1;
    	int n;cin>>n;n--;
    	ans=mul(mul(A[n&(N-1)],B[n>>20]),ans);
    	ans1^=ans.a[0];ans2^=ans.a[3];
    	}
    signed main()
    	{
    	freopen("riemannian.in","r",stdin);
    	freopen("riemannian.out","w",stdout);
    	ios::sync_with_stdio(0);cin.tie(0);
    	init();int t;cin>>t;
    	while(t--)sol();
    	cout<<ans1<<' '<<ans2<<'\n';
    	return 0;
    	}
    
    • 4
      @ 2026-9-28 12:48:52

      水鸟过了?问他怎么做???

      水鸟:光速幂!!!

      tzq 过了?问她怎么做???

      tzq:unsigned long long 循环展开!!!

      我都不会怎么办。

      upd: n≤1.2×106n \leq 1.2 \times 10^6.


      朴素的 O(t×log⁡w)O (t \times \log w) 是 naive 的。

      发现我们可以做到在线回答但是题面没给强制在线。

      我们直接将询问离线,然后排序所有询问,每次将答案矩阵快速幂的幂次改为上一个和这一个的差。

      于是我们做到了 O(tlog⁡t+log⁡w×33)O (t \log t+ \log w \times 3^3)。

      不是哥们这个复杂度都 95 pts TLE 了。

      事已至此。

      const int MAXSIZE=1<<25;
      char buf[MAXSIZE],*p1,*p2;
      #define gc()                                                               \
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
             ? EOF                                                               \
             : *p1++)
      
      ll rd()
      {
      	ll x=0,f=1;
      	char c=gc();
      	while(!isdigit(c))
      	{
      		if(c=='-') f=-1;
      		c=gc();
      	}
      	while (isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=gc();
      	return x*f;
      }
      

      我以峰值 914ms 通过了这道题。

      愿世界没有卡常题。

      什么叫做有一组 n=2×106n = 2 \times 10^6 的数据?

      #include <bits/stdc++.h>
      
      using namespace std;
      using ll = long long;
      
      const int N = 2e6 + 3, MOD = 998244353;
      
      int n, ans1, ans2;
      ll q[N];
      
      int add (int x, int y) {
        return x + y >= MOD ? x + y - MOD : x + y;
      }
      
      struct Matrix {
        int n, m, a[5][5] = {0};
      
        Matrix operator * (const Matrix &p) const {
          Matrix res; res.n = n, res.m = p.m;
          for (int i = 1; i <= n; ++i) {
            for (int k = 1; k <= p.m; ++k) {
              res.a[i][k] = add (res.a[i][k], (1ll * a[i][1] * p.a[1][k] + 1ll * a[i][2] * p.a[2][k] + 1ll * a[i][3] * p.a[3][k]) % MOD);
            }
          }
          return res;
        }
      } F, w[1 << 6];
      
      int main () {
        freopen ("riemannian.in", "r", stdin);
        freopen ("riemannian.out", "w", stdout);
        ios :: sync_with_stdio (0), cin.tie (0);
        F.n = 1, F.m = 3, F.a[1][1] = 2, F.a[1][2] = 0, F.a[1][3] = 3;
        w[0].n = w[0].m = 3, w[0].a[1][1] = w[0].a[2][1] = 2;
        w[0].a[1][2] = w[0].a[3][1] = w[0].a[3][3] = 1;
        for (int i = 1; i < 50; ++i) {
          w[i] = w[i - 1] * w[i - 1];
        }
        cin >> n;
        for (int i = 1; i <= n; cin >> q[i++]) {
        }
        stable_sort (q + 1, q + n + 1), q[0] = 1;
        for (int i = 1; i <= n; ++i) {
          ll y = q[i] - q[i - 1];
          for (int i = 0; (1ll << i) <= y; ++i) {
            if (y >> i & 1) {
              F = F * w[i];
              y ^= 1ll << i;
            }
          }
          ans1 ^= add (F.a[1][2], F.a[1][2] + 1), ans2 ^= F.a[1][1];
        }
        return cout << ans1 << " " << ans2, 0 ^ 0;
      }
      
      
      • @ 2026-9-28 15:55:59

        \cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/\cxm/

    • -5
      @ 2026-9-28 14:17:00

      • 1

      信息

      ID
      55
      时间
      1000ms
      内存
      512MiB
      难度
      8
      标签
      递交数
      175
      已通过
      25
      上传者