ZOJ 3785:What day is that day?(数论)
What day is that day?
Time Limit: 2 Seconds Memory Limit: 65536 KB
It’s Saturday today, what day is it after 11+22+33+...+NN1^1 + 2^2 + 3^3 + ... + N^N11+22+33+...+NN days?
Input
There are multiple test cases. The first line of input contains an integer TTT indicating the number of test cases. For each test case:
There is only one line containing one integer N(1<=N<=1000000000)N (1 <= N <= 1000000000)N(1<=N<=1000000000).
Output
For each test case, output one string indicating the day of week.
Sample Input
2
1
2
Sample Output
Sunday
Thursday
Hint
A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.
Solve
对于1…N1 \dots N1…N,对于777取模后,可以转换成一大串0…60 \dots 60…6的循环,所以11+22+33+...+NN1^1 + 2^2 + 3^3 + ... + N^N11+22+33+...+NN可以转换成:11+22+33+…(N%7)N1^1+2^2+3^3+ \dots (N\%7)^N11+22+33+…(N%7)N,然后我们可以发现,这个式子的值是七个等比数列的前xxx项和的总和。每项公比为num7num^7num7,第一项为nnn^nnn(0≤n≤6)(0\leq n \leq 6)(0≤n≤6)
那么我们只需要统计出0…60\dots 60…6的个数,并利用等比数列的前nnn项和公式计算即可
百度了一下,发现似乎写麻烦了,貌似可以直接打表找规律?而且代码还特别短,想看短代码的自行百度吧,貌似没有找到我这种方法写的QAQ
Code
/*************************************************************************
> Author: WZY
> School: HPU
> Created Time: 2019-04-20 09:49:05
************************************************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=(1<<30);
const ll INF=(1LL*1<<60);
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;y=0;
return a;
}
else
{
int r=exgcd(b,a%b,y,x);
y-=x*(a/b);
return r;
}
}
int inv(int a,int n)
{
int x,y;
exgcd(a,n,x,y);
x=(x%n+n)%n;
return x;
}
int Pow(int a,int b,int c)
{
int res=1;
while(b)
{
if(b&1)
res=res*a%c;
a=a*a%c;
b>>=1;
}
return res;
}
int a[10];
int sum[10];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t;
// 计算第一项的值
for(int i=1;i<7;i++)
sum[i]=Pow(i,7,7);
cin>>t;
int n;
while(t--)
{
ms(a,0);
cin>>n;
// 统计0~6出现的个数
for(register int i=1;i<7;i++)
a[i]=n/7+(n%7>=i);
// 0忽略,初始值为1的个数
int ans=a[1];
int __=0;
for(register int i=2;i<7;i++)
{
// 利用前n项和公式+逆元计算对7取模的结果
int _=(Pow(i,i,7)*((Pow(sum[i],a[i],7)-1+7)%7)*inv(sum[i]-1,7)+7)%7;
__+=_;
if(a[i]==0)
break;
}
ans=(ans+__)%7;
int cnt=(6+ans)%7;
if(cnt==0)
cout<<"Sunday\n";
if(cnt==1)
cout<<"Monday\n";
if(cnt==2)
cout<<"Tuesday\n";
if(cnt==3)
cout<<"Wednesday\n";
if(cnt==4)
cout<<"Thursday\n";
if(cnt==5)
cout<<"Friday\n";
if(cnt==6)
cout<<"Saturday\n";
}
return 0;
}
ZOJ 3785:What day is that day?(数论)的更多相关文章
- ZOJ 3785 What day is that day?(今天是星期几?)
Description 题目描述 It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? 今天是星期六,11 + ...
- ZOJ 3785 What day is that day?(数论:费马小定理)
What day is that day? Time Limit: 2 Seconds Memory Limit: 65536 KB It's Saturday today, what da ...
- zoj 3785 What day is that day? (打表找规律)
题目 思路:比赛的时候有想过找循环节,但是,打表打错了. 后来,看着过了挺多人,就急了, 看了一下别人的时间 耗时都挺长的,就以为不是找规律, 没想到真是找规律,不过,这个题的数据可能挺大的. AC代 ...
- zoj 3785 What day is that day?
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272 打表找规律. #include <cstdio> #incl ...
- zoj 3621 Factorial Problem in Base K 数论 s!后的0个数
Factorial Problem in Base K Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onli ...
- ZOJ 1489 2^x mod n = 1 数论
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=489 题目大意: 给你正整数n,求最小的x使得2^x mod n = 1. 思路 ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- 【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律
转自:http://www.cnblogs.com/kevince/p/3887827.html 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这么一说大家心里肯定有数了吧,“不就是nex ...
- 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+ ...
随机推荐
- linux系统中安装MySQL
linux系统中安装MySQL 检查原来linux系统中安装的版本 rpm -qa | grep mysql 将其卸载掉 以 mysql-libs-5.1.71-1.el6.x86_64 版本为例 r ...
- A Child's History of England.33
To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...
- 4.1 python中调用rust程序
概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...
- 加密解密、食谱、新冠序列,各种有趣的开源项目Github上都有
Github上是我们程序员学习开源代码.提升编程技巧的好地方.好学校,但是除了学习,小伙伴们有没有发现过Github上一些特别有意思的项目呢? 今天TJ君就来和大家分享几个自认为特别有趣的开源项目: ...
- Nginx支持php
目录 一.简介 二.配置 三.测试 四.参数 一.简介 Nginx本身只能解析html文件,但有些网页是php写的,就需要Nginx连接php,将网页解析成html再发给客户端. 配置中将.php 结 ...
- Linux入侵 反弹shell
目录 一.简介 二.命令 三.NetCat 一.简介 黑入服务器很少会是通过账号密码的方式进入,因为这很难破解密码和很多服务器都做了限制白名单. 大多是通过上传脚本文件,然后执行脚本开启一个端口,通过 ...
- 万字教你如何用 Python 实现线性规划
摘要:线性规划是一组数学和计算工具,可让您找到该系统的特定解,该解对应于某些其他线性函数的最大值或最小值. 本文分享自华为云社区<实践线性规划:使用 Python 进行优化>,作者: Yu ...
- CF1097B Petr and a Combination Lock 题解
Content 有一个锁,它只有指针再次指到 \(0\) 刻度处才可以开锁(起始状态如图所示,一圈 \(360\) 度). 以下给出 \(n\) 个操作及每次转动度数,如果可以通过逆时针或顺时针再次转 ...
- CF20C Dijkstra? 题解
Content 给定一张 \(n\) 个点 \(m\) 条边的无向图,请判断是否有一条可行的从 \(1\) 到 \(n\) 的路径,有的话输出长度最短的,没有的话输出 -1. 数据范围:\(2\leq ...
- LuoguB2105 矩阵乘法 题解
Content 给定一个 \(n\times m\) 的矩阵 \(A\) 和一个 \(m\times k\) 的矩阵 \(B\),求两个矩阵相乘得到的矩阵. \(n\times m\) 的矩阵 \(A ...