ZOJ 3785-What day is that day?-数论(费马小定理) / 打表找规律
2016-04-11 13:09
344 查看
http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3785
求 【1^1 +2^2 +3^3 +....+n^n】%7的答案
因为7是素数,根据费马小定理,可以把指数降幂为0~5
根据取模的性质也可以把底数降幂为0~6
因此 指数循环节为6,底数循环节为7,最小公倍数为42,所以表的循环节为42,因此直接暴力算n%42的部分即可
当然你可以打表找规律咯
注意求i^i用pow有精度丢失,可以用快速幂代替
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
string mp[]={"Saturday","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
long long pow_m(long long a,long long b,long long c)
{
long long ans=1,tmp=a;
while(b)
{
if (b&1)
ans=ans*tmp%c;
tmp=tmp*tmp%c;
b>>=1;
}
return ans%c;
}
int main()
{
//freopen("xl_in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int i=1,j=1;
long long ans=0;
int n;
cin>>n;
ans+=(n/42*6)%7;
n=n%42;
for(int k=0;k<n;k++)
{
ans+= pow_m((i++)%7,(j++)%6,7);
}
// printf("%lld\n",ans%7);
cout<<mp[ans%7]<<endl;
}
return 0;
}
求 【1^1 +2^2 +3^3 +....+n^n】%7的答案
因为7是素数,根据费马小定理,可以把指数降幂为0~5
根据取模的性质也可以把底数降幂为0~6
因此 指数循环节为6,底数循环节为7,最小公倍数为42,所以表的循环节为42,因此直接暴力算n%42的部分即可
当然你可以打表找规律咯
注意求i^i用pow有精度丢失,可以用快速幂代替
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
string mp[]={"Saturday","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
long long pow_m(long long a,long long b,long long c)
{
long long ans=1,tmp=a;
while(b)
{
if (b&1)
ans=ans*tmp%c;
tmp=tmp*tmp%c;
b>>=1;
}
return ans%c;
}
int main()
{
//freopen("xl_in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int i=1,j=1;
long long ans=0;
int n;
cin>>n;
ans+=(n/42*6)%7;
n=n%42;
for(int k=0;k<n;k++)
{
ans+= pow_m((i++)%7,(j++)%6,7);
}
// printf("%lld\n",ans%7);
cout<<mp[ans%7]<<endl;
}
return 0;
}
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- Mac怎么不能拷贝文件到U盘
- TCP协议——客户端与服务端一问一答聊天
- Ubuntu 安装vsftp软件(已测试)
- 修改系统时间
- 跋涉不止(37)
- 信息交流平台专业介绍模块的分析(UML)与设计
- cf590A. Median Smoothing
- xUtils 中的HttpUtils全面注释
- Ubuntu安装opencv with cuda
- java 笔录
- UITableViewCell在重用ID时为何加上Static关键字
- html5 笔录
- Maven将项目部署到Tomcat-报错:java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- 简单C三道!
- 撰写英文SCI文章的技巧
- shell变量赋值 不能有空格的原因
- 【机器学习】多元高斯分布下的贝叶斯分类感知器
- PopupWindow弹出框且背景变暗
- 跋涉不止(36)