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 ...
随机推荐
- struts2中valueStack,stackContext以及actionContext的关系
一,首先给出三者的定义 1.valueStack: 里面存放的是Action类中通过set方法设置的属性值(表单传过来的值等),由OGNL框架实现; 2.stackContext: 也是用来存值的,s ...
- angularjs post
/** * POST 1 * $http.post('http://localhost:8001/quickstart/task/create', { newTask: newTask }) */ / ...
- 度娘果然毫无节操,纯粹就是order by 广告费 desc
度娘果然毫无节操,纯粹就是order by 广告费 desc 必应搜索出来排第一,度娘根本就找不到在哪....
- [Xamarin.Android] 使用Component套件
[Xamarin.Android] 使用Component套件 前言 在Xamarin中,可以将自己开发的项目包装成为Component套件发布至Xamarin Component Store,来提供 ...
- [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider
[ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...
- javascript --- 设计模式之单体模式(一)
单体是一个用来划分命名空间并将一些相关的属性与方法组织在一起的对象,如果她可以被实例化的话,那她只能被实例化一次(她只能嫁一次,不能二婚). 单体模式是javascript里面最基本但也是最有用的模式 ...
- C#进制转换
//十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...
- android 内存泄露调试
一.概述 1 二.Android(Java)中常见的容易引起内存泄漏的不良代码 1 (一) 查询数据库没有关闭游标 2 (二) 构造Adapter时,没有使用缓存的 convertView 3 (三) ...
- Git使用之设置SSH Key
设置SSH Key 1. 检查是否已经有SSH Key. $cd ~/.ssh 如果说没有这个目录,就直接看第三步 2. 备份 3. 生成一个新的SSH. $ssh-keygen - ...
- 【未解决】eclipse未自动引入maven依赖
删掉maven本地库,重新编译项目,刷新eclipse后,发现工程上打叉,查看build path,依赖全没有引入,不知为何,暂无解