Factorials
题意:求N!结果中最后一位不为0的数字。如5! = 120,那么结果中最后一位不为0的数字就是2
解题思路:
- 因为N <= 4220,所以在每一步中只需要保存最后四位就可以了。
- 初始化result = 1,遍历1~N,每次用result * i,用result保存得到的结果的后四位不为0的数字。
- 取最后得到结果result的最后一位即可
代码:
/*
ID: zc.rene1
LANG: C
PROG: fact4
*/#include<stdio.h>
#include<stdlib.h>
#include<string.h>int main(void)
{FILE *fin, *fout;int result;int N, i;fin = fopen("fact4.in", "r");fout = fopen("fact4.out", "w");/*get input*/fscanf(fin, "%d", &N);/*begin DP*/result = 1;for (i=1; i<=N; i++){result *= i;while (result % 10 == 0){result /= 10;}result %= 10000;}/*print result*/fprintf(fout, "%d\n", result % 10);return 0;
}
Factorials相关推荐
- USACO / Factorials (简单模拟)
USACO/Factorials Factorials阶乘 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 阶乘会变大得很快,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了. ...
- UVA160 Factors and Factorials【筛选法】
The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. ...
- C语言Matrix编程题——[Recursion]D. Liang 8.1 Computing factorials
[Recursion]D. Liang 8.1 Computing factorials Description: 用以下的函数完成一个计算阶乘的函数 long factorial(int n) In ...
- usaco Factorials
想到除2除5,没想到除后怎么办 /* ID:jinbo wu TASK: fact4 LANG: C++ */ #include<bits/stdc++.h> using namespac ...
- Factorials 阶乘
Description N的阶乘写作N!表示小于等于N的所有正整数的乘积.阶乘会很快的变大,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了.你的任务是找到阶乘最后面的非零位.举个例 ...
- Codeforces 1646C Factorials and Powers of Two
0x10 题目链接 cf 1646c 0x20 题目 0x21 Tag 状态压缩,枚举 0x22 题目描述 0x23 翻译 给出一个数n,是否能将其拆解成多个**(共k个)**2的幂次与阶乘的和,求k ...
- CF 1646C Factorials and Powers of Two
题目 思路 只有2的指数阶和n的阶层所构成 数据范围是101210^{12}1012,恰好在2402^{40}240和15!15!15!以内 总所周知,每一个整数都可以转换为相应的二进制数, 对应这道 ...
- CodeForces - 1646C Factorials and Powers of Two
看了大佬的博客才会写的 传送门 #include<bits/stdc++.h> using namespace std;#define int long longint lowbit(in ...
- Factors and Factorials
题目: 输入一个数N,计算N的阶乘的质因数的个数,按质数从小到大排列输出各个质数的个数. Input: 测试用例的个数T(0<T<=100) 要求的数N(2<=N<=100) ...
最新文章
- [bat] 使用bat文件保证指定程序运行
- eclipse开发android手机定位
- 前端开发者必备的代码开源平台,记得收藏转发!
- ES6新特性之函数优化-箭头函数
- 玩转Web之servlet(四)---B/S是如何使用http协议完成通信过程的
- python:lambda、filter、map、reduce
- python制作考试系统_Python系统学习 - Pytest单元测试框架
- Linux中 print用法,linux之find中的-print0和xargs中-0用法
- 阿里之后 “安卓终结者”来了!
- 比赛总结——atcoder beginner contest 109
- IOS开发之日期时间格式化字符说明
- c++ file* 句柄泄漏_C/C++连接MySql数据库使用总结
- VMvare虚拟机如何删除安装的ubuntu操作系统
- 20190605每日一句你的态度决定了你的人生高度
- JavaScript 计算器
- Gantt - attachEvent事件监听 - 一个参数事件
- 数据库ASM管理-ASM文件
- 5个理由告诉你为什么用NAS网络存储
- win10所有浏览器打开一段时间后会自动关闭-未解决
- A Game of Thrones(50)