日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

湖南大学第十四届ACM程序设计新生杯(重现赛)L-The Digits String (矩阵快速幂)

發布時間:2024/4/18 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 湖南大学第十四届ACM程序设计新生杯(重现赛)L-The Digits String (矩阵快速幂) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

題目描述

Consider digits strings with length n, how many different strings have the sum of digits are multiple of 4?

輸入描述:

There are several test cases end with EOF. For each test case, there is an integer in one line: n, the length of the digits string. (1≤n≤1,000,000,000).

輸出描述:

For each case, output the number of digits strings with length n have the sum of digits are multiple of 4 in one line. The numbers maybe very large, you should output the result modular 2019.

輸入

1
2
3
4

輸出

3
25
249
479

思路

長度為1有四種情況:

  • %4 == 0 有3個
  • %4 == 1 有3個
  • %4 == 2 有2個
  • %4 == 3 有2個
  • 計算長度為N的分為四種情況:
    令長度為N-1模4為{0, 1, 2, 3}的數量為T0, T1, T3, T4

  • %4 == 0
    (%4=0) * T0 + (%4=1) * T3 + (%4=2) * T2 + (%4=3) * T1
  • %4 == 1
    (%4=0) * T1 + (%4=1) * T0 + (%4=2) * T3 + (%4=3) * T2
  • %4 == 2
  • (%4=0) * T2 + (%4=1) * T1 + (%4=2) * T0 + (%4=3) * T3
  • %4 == 3
    (%4=0) * T3 + (%4=1) * T2 + (%4=2) * T1 + (%4=3) * T0
  • 根據這個思路可以用矩陣快速冪加快一下

    #include <bits/stdc++.h> #define LL long long #define P pair<int, int> #define lowbit(x) (x & -x) #define mem(a, b) memset(a, b, sizeof(a)) #define REP(i, n) for (int i = 1; i <= (n); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define N 100005 using namespace std;void mul(LL x[], LL y[]) {LL t[4];t[0] = x[0] * y[0] + x[1] * y[3] + x[3] * y[1] + x[2] * y[2];t[1] = x[1] * y[0] + x[0] * y[1] + x[2] * y[3] + x[3] * y[2];t[2] = x[0] * y[2] + x[2] * y[0] + x[1] * y[1] + x[3] * y[3];t[3] = x[0] * y[3] + x[3] * y[0] + x[1] * y[2] + x[2] * y[1];rep (i, 4) x[i] = t[i] % 2019; }LL quick(LL x) {LL b[4] = {3, 3, 2, 2};LL a[4] = {3, 3, 2, 2};while (x) {if (x & 1) mul(a, b);mul(b, b);x >>= 1;}return a[0]; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endifLL n;while (scanf("%lld", &n) != EOF) {LL ans = quick(n-1);printf("%d\n", ans % 2019);}return 0; }

    總結

    以上是生活随笔為你收集整理的湖南大学第十四届ACM程序设计新生杯(重现赛)L-The Digits String (矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。