HDOJ 3123 GCC (数学)
来源:互联网 发布:汇编语言编程 编辑:程序博客网 时间:2024/06/10 13:06
GCC
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4476 Accepted Submission(s): 1475
Problem Description
The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages. But it doesn’t contains the math operator “!”.
In mathematics the symbol represents the factorial operation. The expression n! means "the product of the integers from 1 to n". For example, 4! (read four factorial) is 4 × 3 × 2 × 1 = 24. (0! is defined as 1, which is a neutral element in multiplication, not multiplied by anything.)
We want you to help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!)%m
In mathematics the symbol represents the factorial operation. The expression n! means "the product of the integers from 1 to n". For example, 4! (read four factorial) is 4 × 3 × 2 × 1 = 24. (0! is defined as 1, which is a neutral element in multiplication, not multiplied by anything.)
We want you to help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!)%m
Input
The first line consists of an integer T, indicating the number of test cases.
Each test on a single consists of two integer n and m.
Each test on a single consists of two integer n and m.
Output
Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m.
Constrains
0 < T <= 20
0 <= n < 10^100 (without leading zero)
0 < m < 1000000
Constrains
0 < T <= 20
0 <= n < 10^100 (without leading zero)
0 < m < 1000000
Sample Input
110 861017
Sample Output
593846题意:给你一个n,然后求0到n的阶乘相加对m取余。思路:我们会发现当n大于等于m的时候,n!%m==0,所以只需要算比m小的数的阶乘之和对m取余就可以了,因为n的长度可达到100位,所以用字符串,另外多一个特判。总结:不能被那个数吓到,仔细写一下会发现没那么麻烦。。ac代码:#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define mod 1000000007using namespace std;char s[110];int main(){int t,n,m,i,j,num,a;int cas=0;scanf("%d",&t);while(t--){scanf("%s%d",s,&m);int len=strlen(s);if(len>=7){num=m-1;LL ans=0;LL k=1;for(i=1;i<=num;i++){k=k*i%m;ans=(ans+k)%m;}printf("%lld\n",(ans+1)%m);continue;}num=0;for(i=0;i<len;i++)num=num*10+s[i]-'0';if(num>=m)num=m-1;LL ans=0;LL k=1;for(i=1;i<=num;i++){k=k*i%m;ans=(ans+k)%m;}printf("%lld\n",(ans+1)%m);}return 0;}
0 0
- HDOJ 3123 GCC (数学)
- HDOJ 3123 GCC
- hdoj-3123-GCC
- HDU 3123 GCC(数学)
- hdu 3123 GCC (数学)
- hdoj 1214 圆桌会议【数学】
- 【数学】 HDOJ 5051 Fraction
- 【数学】 HDOJ 5035 Delivery
- Hdoj 1719 Friend 【数学】
- HDOJ 2534 Score(数学)
- HDOJ 1008-Elevator【数学】
- HDOJ 5605-geometry【数学】
- hdoj 5211 Mutiple 【数学】
- hdoj 5670 Machine 【数学】
- HDOJ 1134 数学+大数
- hdoj GCC 3123 (大数阶乘取余&转换)
- hdoj--3123--GCC(技巧阶乘取余)
- gcc 数学函数的使用
- 华为机试——字符串加密
- 输出汉诺塔的移动步骤
- Android 实现APP开屏广告
- 华为机试——小球落地
- IOS开发系列——Masonry手写Autolayout专题【整理,部分原创】
- HDOJ 3123 GCC (数学)
- ansible安装及问题解决
- android 之定义的栈来管理activity
- Android Studio的常见快捷键
- javascript基础一
- Property 属性动画入门
- 华为机试——判断两个IP时候属于同一个子网
- 各级数整理
- 编程基础学习地址收藏