链接: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?

输入描述:

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

输入

复制

1
2
3
4

输出

复制

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <bitset>
using namespace std; #define LL long long
const int matrix_size = 4;
const int MOD = 2019; int add(int a, int b)
{
a += b;
if(a >= MOD)
{
return a - MOD;
}
return a;
} struct Matrix
{
int size;
int num[matrix_size][matrix_size]; void operator=(const Matrix &m)
{
for(int i = 0; i < size; ++i)
{
memcpy(num[i], m.num[i], sizeof(int) * size);
}
} void Init()
{
for(int i = 0; i < size; ++i)
{
memset(num[i], 0, sizeof(int) * size);
num[i][i] = 1;
}
} void Set()
{
size = 4;
memset(num, 0, sizeof(num));
for(int j = 0; j < 4; ++j)
{
for(int i = 0; i < 10; ++i)
{
++num[(j + i) % 4][j];
}
}
} void operator*=(const Matrix &m)
{
static Matrix ans;
ans.size = size;
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < size; ++j)
{
ans.num[i][j] = 0;
for(int k = 0; k < size; ++k)
{
ans.num[i][j] = add(ans.num[i][j], (LL)num[i][k] * m.num[k][j] % MOD);
}
}
}
(*this) = ans;
} void fast_pow(LL n)
{
static Matrix ans;
ans.size = size;
for(ans.Init(); n != 0; n >>= 1)
{
if((n & 1) == 1)
{
ans *= (*this);
}
(*this) *= (*this);
}
(*this) = ans;
}
}; int n;
Matrix m; int main()
{
#ifdef Dmaxiya
freopen("test.txt", "r", stdin);
#endif // Dmaxiya
ios::sync_with_stdio(false); while(scanf("%d", &n) != EOF)
{
m.Set();
m.fast_pow(n);
printf("%d\n", m.num[0][0]);
} return 0;
}

标答

•#include <stdio.h>

•#define M 2019

•#define L 4

•#define LP(i) for(i=0;i<L;++i)

•void Product(int x[L][L],int y[L][L],int z[L][L]){

•    int i,j,k,w[L][L]={};

•    LP(i)LP(j)LP(k)w[i][j]=(w[i][j]+x[i][k]*y[k][j])%M;

•    LP(i)LP(j)z[i][j]=w[i][j];}

•

•void FPow(int n,int r[L][L]){

•    int i,j,A[L][L]={{,,,},{,,,},{,,,},{,,,}};

•    LP(i)LP(j)r[i][j]=i==j?:;

•while(n){

•if(n&)Product(r,A,r);

•Product(A,A,A);

•    n>>=;}}

•int cal(int n){

•int r[L][L];

•FPow(n,r);

•    return    (*r[][]+*r[][]+*r[][]+*r[][])%M;}

•

•int main(){

•int n;

•while(scanf("%d",&n)!=EOF&&n>)

•printf("%d\n",cal(n-));

•return ;}
解题思路(续)
• 矩阵




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

•int cal(int n){
•int r1,r2;
• r1=FPow(,n);
• r2=((FPow(,n/)*et[n%])%M+M)%M;
• return ((r1+r2)*)%M;
• }
•int main(){
•int n,i;
• rt[][]=;rt[][]=;
• for(i=;i<;++i){
• rt[][i]=(rt[][i-]*rt[][i-])%M;
• rt[][i]=(rt[][i-]*rt[][i-])%M;}
•while(scanf("%d",&n)!=EOF&&n>)
•printf("%d\n",cal(n));
•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. web开发中SESSION的本质

    有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制.我们先简单的了解一些http的知识,从而理解该协议的无 ...

  2. 2018-12-25-win10-uwp-显示SVG

    title author date CreateTime categories win10 uwp 显示SVG lindexi 2018-12-25 10:37:5 +0800 2018-2-13 1 ...

  3. 关于windows服务器配置

    #我是用的window service2008系统,在配置服务器时由于是用php进行搭建 #首先我安装好phpstudy,通过服务器ip访问,显示了个helloworld,我查看了phpstudy里的 ...

  4. 03机器学习实战之决策树scikit-learn实现

    sklearn.tree.DecisionTreeClassifier 基于 scikit-learn 的决策树分类模型 DecisionTreeClassifier 进行的分类运算 http://s ...

  5. 将两个列表合并为字典_其中一个列表为Key_一个列表为Value

    #定义两个列表 list1 = range(0,10) list2 = range(10,20) #合并为字典,调用dict(zip()) dict_name = dict(zip(list1,lis ...

  6. 记录卸载5.7版本MySQL并安装5.6版本MySQL

    新版本有些问题很烦,也没时间去找解决办法,只好用回5.6,首先卸载6.7的MySQL: sudo apt-get autoremove --purge mysql-server-* apt remov ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【串线篇】spring boot日志框架

    一.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息:日志框架 ...

  9. php array_search()函数 语法

    php array_search()函数 语法 作用:在数组中搜索某个键值,并返回对应的键名.dd马达生产厂家 语法:array_search(value,array,strict) 参数: 参数 描 ...

  10. Solr基本命令

    启动Solr 安装Solr后,进入到Solr主目录中的bin文件夹,并使用以下命令启动Solr. [Hadoop@localhost ~]$ cd [Hadoop@localhost ~]$ cd S ...