洛谷 P1134 阶乘问题
2024-08-30 01:37:08
一开始只保留最后一位,交上去29
#include<cstdio>
#include<cmath>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
int main()
{
int n, ans = 1;
scanf("%d", &n);
_for(i, 2, n)
{
ans *= i;
while(ans % 10== 0) ans /= 10;
ans %= 10;
}
printf("%d\n", ans % 10);
return 0;
}
然后通过数据发现
然后我就把真正的数和我保留最后一位的数比较一下,发现有一组数据答案有问题
i = 14 sum = 87178291200 ans = 2
i = 15 sum = 130767436800 ans = 3
这组数据是关键
如果按照最后一位的话,2 * 15 = 30, 取3
但是我们把sum手算乘上15会发现
87178291200
15
----------------
…… 60
……2
-----------------
……80
这里可以看到,影响到末尾的位的是后两位,而不仅仅是最后一位
所以以此类推,乘数最大为10的8次方方级别,所以我就只需要存
去0情况下的最后8位数就好了。8位数乘8位数int会炸,所以开longlong
#include<cstdio>
#include<cmath>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int base = 1e8;
int main()
{
int n; ll ans = 1;
scanf("%d", &n);
_for(i, 2, n)
{
ans *= i;
while(ans % 10== 0) ans /= 10;
ans %= base;
}
printf("%lld\n", ans % 10);
return 0;
}
最新文章
- csuoj 1116: Kingdoms
- MySQL的左连接、右连接和全连接的实现
- iOS阶段学习第27天笔记(UIButton-UIImageView的介绍)
- git merge 和 git rebase 小结
- sharepoint的导航开发
- svn server
- Android 学习笔记 Service服务与远程通信...(AIDL)
- Ajax学习(1)-简单ajax案例
- linux 安装firefox
- knowledge about apache
- let 与 expr Shell运算比较 let强强胜出
- gevent拾遗
- 更优雅的方式: JavaScript 中顺序执行异步函数
- LeetCode(15. 三数之和)
- centos7 yum安装ffmpeg,以及ffmpeg的简单用法
- Scrapy-Splash的介绍、安装以及实例
- Android学习笔记----Java字符串MD5加密
- 利用autocomplete.js实现仿百度搜索效果(ajax动态获取后端[C#]数据)
- Mysql 函数使用记录(三)——UNIX_TIMESTAMP() 、UNIX_TIMESTAMP(date)
- mime模块响应或设置Node.js的Content-Type头
热门文章
- 《黑白团团队》第八次团队作业:Alpha冲刺 第一天
- google浏览器中 查看记住的账号和密码
- 解决was6版本号过期问题
- 详解Mysql分布式事务XA(跨数据库事务)
- UVa11183 - Teen Girl Squad(最小树形图-裸)
- SOA概念具体解释
- 2015.03.12,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯”学习笔记 SESSION 25
- [SCOI 2009] 生日快乐
- 26.QT颜色与布局
- Git 学习笔记(三)