链接:https://ac.nowcoder.com/acm/contest/338/L
来源:牛客网

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

输入描述:

  1. 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. (1n1,000,000,000).

输出描述:

  1. 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

输入

复制

  1. 1
  2. 2
  3. 3
  4. 4

输出

复制

  1. 3
  2. 25
  3. 249
  4. 479
  1. 题目大意
  2. 求长度为n的数字串中,有多少个这样数字串,其数字
  3. 之和是4的倍数(包括0
  4. 输入:每组测试数据一行,包含一个正整数n c )
  5. 输出:对于每组测试数据,输出一行,包含一个整数, 表示有多少个这样数字串,其数字之和是4的倍数(包 0)。因为这个结果很大,所以将值模2019输出
  6.  
  7. 本题快速幂,复杂度为Ologn)。标程在1000组测试 数据下的运行时间约为60毫秒(第二标程运行时间约 30毫秒)。建议时间限制为1秒,空间限制为64M

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cfloat>
  6. #include <climits>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. #include <bitset>
  17. using namespace std;
  18.  
  19. #define LL long long
  20. const int matrix_size = 4;
  21. const int MOD = 2019;
  22.  
  23. int add(int a, int b)
  24. {
  25. a += b;
  26. if(a >= MOD)
  27. {
  28. return a - MOD;
  29. }
  30. return a;
  31. }
  32.  
  33. struct Matrix
  34. {
  35. int size;
  36. int num[matrix_size][matrix_size];
  37.  
  38. void operator=(const Matrix &m)
  39. {
  40. for(int i = 0; i < size; ++i)
  41. {
  42. memcpy(num[i], m.num[i], sizeof(int) * size);
  43. }
  44. }
  45.  
  46. void Init()
  47. {
  48. for(int i = 0; i < size; ++i)
  49. {
  50. memset(num[i], 0, sizeof(int) * size);
  51. num[i][i] = 1;
  52. }
  53. }
  54.  
  55. void Set()
  56. {
  57. size = 4;
  58. memset(num, 0, sizeof(num));
  59. for(int j = 0; j < 4; ++j)
  60. {
  61. for(int i = 0; i < 10; ++i)
  62. {
  63. ++num[(j + i) % 4][j];
  64. }
  65. }
  66. }
  67.  
  68. void operator*=(const Matrix &m)
  69. {
  70. static Matrix ans;
  71. ans.size = size;
  72. for(int i = 0; i < size; ++i)
  73. {
  74. for(int j = 0; j < size; ++j)
  75. {
  76. ans.num[i][j] = 0;
  77. for(int k = 0; k < size; ++k)
  78. {
  79. ans.num[i][j] = add(ans.num[i][j], (LL)num[i][k] * m.num[k][j] % MOD);
  80. }
  81. }
  82. }
  83. (*this) = ans;
  84. }
  85.  
  86. void fast_pow(LL n)
  87. {
  88. static Matrix ans;
  89. ans.size = size;
  90. for(ans.Init(); n != 0; n >>= 1)
  91. {
  92. if((n & 1) == 1)
  93. {
  94. ans *= (*this);
  95. }
  96. (*this) *= (*this);
  97. }
  98. (*this) = ans;
  99. }
  100. };
  101.  
  102. int n;
  103. Matrix m;
  104.  
  105. int main()
  106. {
  107. #ifdef Dmaxiya
  108. freopen("test.txt", "r", stdin);
  109. #endif // Dmaxiya
  110. ios::sync_with_stdio(false);
  111.  
  112. while(scanf("%d", &n) != EOF)
  113. {
  114. m.Set();
  115. m.fast_pow(n);
  116. printf("%d\n", m.num[0][0]);
  117. }
  118.  
  119. return 0;
  120. }

标答

  1. •#include <stdio.h>
  2.  
  3. •#define M 2019
  4.  
  5. •#define L 4
  6.  
  7. •#define LP(i) for(i=0;i<L;++i)
  8.  
  9. void Product(int x[L][L],int y[L][L],int z[L][L]){
  10.  
  11. int i,j,k,w[L][L]={};
  12.  
  13. LP(i)LP(j)LP(k)w[i][j]=(w[i][j]+x[i][k]*y[k][j])%M;
  14.  
  15. LP(i)LP(j)z[i][j]=w[i][j];}
  16.  
  17.  
  18. void FPow(int n,int r[L][L]){
  19.  
  20. int i,j,A[L][L]={{,,,},{,,,},{,,,},{,,,}};
  21.  
  22. LP(i)LP(j)r[i][j]=i==j?:;
  23.  
  24. while(n){
  25.  
  26. if(n&)Product(r,A,r);
  27.  
  28. Product(A,A,A);
  29.  
  30. n>>=;}}
  31.  
  32. int cal(int n){
  33.  
  34. int r[L][L];
  35.  
  36. FPow(n,r);
  37.  
  38. return (*r[][]+*r[][]+*r[][]+*r[][])%M;}
  39.  
  40.  
  41. int main(){
  42.  
  43. int n;
  44.  
  45. while(scanf("%d",&n)!=EOF&&n>)
  46.  
  47. printf("%d\n",cal(n-));
  48.  
  49. return ;}
  1. 解题思路(续)
  2. 矩阵




  3. 有四个特征根,分别是0,,+i,-i
  4. 所以an=x10n+ycos(nπ/)+zsin(nπ/),根据
  5. a1=,a2=,a3=,可得:
  6. an=(10n+2sqrt()ncos(nπ/))/;
  7. 同样快速幂求得答案。
  1. •#include <stdio.h>
  2. •#define M 2019
  3. int rt[][],et[]={,,,-,-,-,,};
  4. int FPow(int f,int n){
  5. int r=,m=;
  6. while(n){
  7. if(n&)r=(r*rt[f][m])%M;
  8. m++;
  9. n>>=;}
  10. return r;}

  11. int cal(int n){
  12. int r1,r2;
  13. r1=FPow(,n);
  14. r2=((FPow(,n/)*et[n%])%M+M)%M;
  15. return ((r1+r2)*)%M;
  16. }
  17. int main(){
  18. int n,i;
  19. rt[][]=;rt[][]=;
  20. for(i=;i<;++i){
  21. rt[][i]=(rt[][i-]*rt[][i-])%M;
  22. rt[][i]=(rt[][i-]*rt[][i-])%M;}
  23. while(scanf("%d",&n)!=EOF&&n>)
  24. printf("%d\n",cal(n));
  25. return ;}

STZG的代码

L The Digits String(没有写完,有空补)的更多相关文章

  1. HDU 4640 状态压缩DP 未写完

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 解题思路: 首先用一个简单的2^n*n的dp可以求出一个人访问一个给定状态的最小花费,因为这i个 ...

  2. 不写完不让回家的JQuery的事件与动画

    在这看不见太阳的小黑屋里,苦逼的一天又开始了 好了闲话我也就不扯了,接下来我就来说说我对jQuery事件和动画的理解吧!!! 还是得再扯两句,我们敬爱的,Y老师讲完了,jQuery事件和动画,对着我们 ...

  3. 刚写完的商城erp + 这个商城前台,新鲜出炉。自己1个人写, 包括php框架和前端html页面.

    刚写完的商城erp + 这个商城前台,新鲜出炉.自己1个人写, 包括php框架和前端html页面. 刚写完的商城erp + 这个商城前台,新鲜出炉.自己1个人写, 包括php框架和前端html页面.

  4. Scrum:The Definition of Done —— 作业有没有写完呢?

    Scrum:The Definition of Done -- 作业有没有写完呢?_苗得雨_新浪博客 http://blog.sina.com.cn/s/blog_59450ffc0102eiai.h ...

  5. 用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存

    问题描述:用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存,但是在putty一按下就不能再输入了.后来查找到:ctrl+s 是putty的一个命令大概是这样子 ...

  6. Word 双栏排版最后多一页空白页删不掉、左栏文字没写完就到右栏了

    1. 问题 问题:Word双栏排版,最后多一页空白页,删不掉.如图: 原因分析:删不掉是因为末尾文字处其实有个下一页分节符,只不过可能看不到. 如何清晰的看到? 视图 > 大纲,就可以看到了.如 ...

  7. String的源码理解(未写完)

    String本质上是一个char数组(jdk 9之后是byte数组),并且是一个声明为final的数组,并且String的不可变也是通过这种把数组声明为final来实现的 public final c ...

  8. QBXT Day2主要是数据结构(没写完先占坑)

    简单数据结构 本节课可能用到的一些复杂度: O(log n). 1/1+1/1/.....1/N+O(n log n) 在我们初学OI的时候,总会遇到这么一道题. 给出N次操作,每次加入一个数,或者询 ...

  9. 写完代码就去吃饺子|The 10th Henan Polytechnic University Programming Contest

    河南理工大学第十届校赛 很久没有组队打比赛了,好吧应该说很久没有写题了, 三个人一起玩果然比一个人玩有趣多了... 前100分钟过了4题,中途挂机100分钟也不知道什么原因,可能是因为到饭点太饿了?, ...

随机推荐

  1. jvm监控和诊断工具

    大牛写的Java的OOM Killer:https://www.jianshu.com/p/4645254be259 强烈推荐 总的参考链接:https://cloud.tencent.com/dev ...

  2. 关于discuz论坛邮箱配置

    Discuz后台可以进行邮件设置,实现网站自动发送邮件给用户的邮箱. 在Discuz邮件设置,经常使用25端口普通发送邮件.为了数据安全,我们也可以使用SSL加密发送,设置方法很简单,只需按照下图进行 ...

  3. tailf 跟踪日志文件

    1.命令功能 tailf 跟踪日志文件增长,作用跟tail –f相同.tailf将输出文件的最后10行,然后等待文件增长. 2.语法格式 tailf  option  file 参数说明 参数 参数说 ...

  4. 第五周作业—N42-虚怀若谷

    一.查找/etc目录下大于1M且类型为普通文件的所有文件 [root@centos7 ~]# find /etc -type f -size +1M -exec ls -lh {} \; -r--r- ...

  5. spring boot构建

    1.新建Maven工程 1.File-->new-->project-->maven project 2.webapp 3.工程名称 k3 2.Maven 三个常用命令 选 项目右击 ...

  6. CocoaPods进阶:本地包管理

    http://www.iwangke.me/2013/04/18/advanced-cocoapods/ 粉笔网的iOS工程师唐巧曾经写过一篇blog<使用CocoaPods来做iOS程序的包依 ...

  7. DHCP服务器怎么设置怎么启动

    DHCP:动态主机配置协议,服务器用于为网络中的客户端自动分配IP地址.这种方法避免了由于手动配置IP地址导致的IP地址冲突问题,同时也减少了网络管理员的工作量. 工具/原料 在配置DHCP服务器时, ...

  8. POI 单元格类型CellType

    1.单元格类型 单元格的内容决定了单元格的类型,POI中定义的7种单元格类型: 2.示例 cell_0.setCellType(CellType.STRING);//字符串 日期数据对应的单元格类型是 ...

  9. C#连redis

    引入 Microsoft.Extensions.Caching.Redis其实就是封装了StackExchange.redis 控制台例子: class Program { public static ...

  10. Integer自动装箱和拆箱

    Integer a=3;   =>    Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...