2014 Super Training #4 G What day is that day? --两种方法
原题: ZOJ 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785
题意:当天是星期六,问经过1^1+2^2+3^3....+n^n天后是星期几?
这题开始以为是这种式子的求和问题,翻了半天没翻到公式。结果没搞出来。后来发现有两种方法。
第一种方法: 找规律
打表可以看出,这些数的结果出现42一循环,所以直接就处理出前42个,后面的就用前面的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 20007 int sum[];
string ss[] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};\ void init()
{
int n,i,j;
sum[] = ;
for(n=;n<=;n++)
{
int flag = n%;
int ans = ;
for(j=;j<=n;j++)
ans = (ans*flag)%;
sum[n] = ans;
}
for(i=;i<=;i++)
sum[i] += sum[i-];
} int main()
{
int i,j,t,n,ans;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans = (((n/)%*(sum[]%))% + sum[n%]%)%;
cout<<ss[ans]<<endl;
}
return ;
}
第二种方法: 矩阵乘法
先把底数对7取模,得出1^1+1^8+1^15+...+ 2^2+2^9+2^16+...
然后就可以分成7组,分别用矩阵加速计算结果,关键就在矩阵的构造了,这个构造我也没太搞懂:
摘自AB的博客。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 107 char s[][]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; struct Matrix
{
int m[][];
}; Matrix Mul(Matrix a,Matrix b)
{
Matrix c;
memset(c.m,,sizeof(c.m));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
c.m[i][j] += ((a.m[i][k]*b.m[k][j])% + )%;
return c;
} int fastm(int a,int b)
{
int res = ;
while(b)
{
if(b&)
res = (res*a)%;
a = (a*a)%;
b >>= ;
}
return res;
} Matrix MPow(Matrix &res,Matrix a,int n) //第二种写法
{
while(n)
{
if(n&)
res = Mul(res,a);
n>>=;
a = Mul(a,a);
}
return res;
} int main()
{
Matrix res,tmp;
int t,n,i;
int sum,num;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum = ;
for(i=;i<=;i++)
{
res.m[][] = res.m[][] = fastm(i,i)%;
res.m[][] = res.m[][] = ;
tmp.m[][] = tmp.m[][] = fastm(i,)%;
tmp.m[][] = ;
tmp.m[][] = ;
num = n/;
if(n% >= i)
num++;
if(num > )
{
num--;
MPow(res,tmp,num);
sum = (sum + res.m[][])%;
}
}
printf("%s\n",s[sum]);
}
return ;
}
2014 Super Training #4 G What day is that day? --两种方法的更多相关文章
- 2014 Super Training #8 G Grouping --Tarjan求强连通分量
原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- 2014 Super Training #6 G Trim the Nails --状态压缩+BFS
原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...
- 2014 Super Training #7 C Diablo III --背包问题(DP)
原题: ZOJ 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 一个带有一些限制的背包问题. 假设在没有限 ...
- 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS
原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- 2014 Super Training #6 F Search in the Wiki --集合取交+暴力
原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
随机推荐
- spring事件通知机制详解
优势 解耦 对同一种事件有多种处理方式 不干扰主线(main line) 起源 要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类: ApplicationEventPub ...
- jQuery中的事件冒泡
1.什么是冒泡 eg: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...
- The main concepts
The MVC application model A Play application follows the MVC architectural pattern applied to the we ...
- 开启Windows Server 2008 R2上帝模式
TAG标签: 摘要:这个“God Mode” 应该大部分的网友都听过了,只是在 Windows Server 2008 R2 上也支持此一功能.启用方式非常简单,在桌面新建一个文件夹,命名为: God ...
- SharePoint 2013 内容部署功能简介
在之前的项目中,当客户有新的需求的时候,我们通常在测试环境上开发或者实施,然后手动在生产环境再弄一次.当发现内容部署这个东西,才知道这样是多么不合理的.我们可以创建两个网站集,一个用来修改,然后通过计 ...
- Jenkins部署.net自动化构建
1.环境部署: windows server 2008R2环境 2.相关软件 SVN(源代码管理器:jenkins通过插件从源代码管理器下载代码) Jenkins(主角)地址:http://f ...
- 基础学习day07---面向对象三---继承,接口与 抽象类
一.继承 1.1.继承概念 将对象的共性抽取出来.提取出一个单独的类. 继承使用复用以前的代码非常容易,能够大大的缩短开发周期,降低开发成本,同时增加程序的易维护性 继承使重一个类A能够直接使用另外一 ...
- iOS屏幕适配知识
一.旋转处理 第一步:注册通知 [[NSNotificationCenter defaultCenter] addObserver:self ...
- git不常用命令
1.删除远程分支 git remote origin :分支名 [解释:意思是提交一个空分支到远程分支] ===持续更新
- Android App的架构设计:从VM、MVC、MVP到MVVM
随着Android应用开发规模的扩大,客户端业务逻辑也越来越复杂,已然不是简单的数据展示了.如同后端开发遇到瓶颈时采用的组件拆分思想,客户端也需要进行架构设计,拆分视图和数据,解除模块之间的耦合,提高 ...