数学知识 同余与模算术
来源:互联网 发布:淘宝剑三金币 编辑:程序博客网 时间:2024/06/05 22:55
为什么会接触到这个数学知识?
还得从一个OJ题目说起。。。。。。
Problem K
Description
FunnyAC likes mathematics very much. He thinks mathematics is very funny and beautiful.When he solved a math problem he would be very happy just like getting accepted in ACM.Recently, he find a very strange problem.Everyone know that the sum of sequence from 1 to n is n*(n + 1)/2. But now if we create a sequence which consists of the sum of sequence from 1 to n. The new sequence is 1, 1+ 2, 1+2+3, …. 1+2+…+n. Now the problem is that what is the sum of the sequence from1 to 1+2+…+n .Is it very simple? I think you can solve it. Good luck!
Input
The first line contain an integer T .Then T cases followed. Each case contain an integer n (1 <= n <= 10000000).
Output
For each case,output the sum of first n items in the new sequence. Because the sum is very larger, so output sum % 20090524.
这个题如果使用递归直接算的话,数据绝对会爆掉。。。。。。。
但是,可以自己推出公式 Sn = n(n+1)(n+2)/6;
然而数据依然爆掉。。。。。。
只能求助汝佳哥了,查到紫书有个同余与模算术这节。
公式如下
(a+b) mod n=((a mod n)+(b mod n)) mod n
(a-b) mod n=((a mod n)-(b mod n)+n) mod n
ab mod n=(a mod n)(b mod n) mod n
#include <stdio.h> int main() { int t; scanf("%d",&t); while(t--) { long long n; scanf("%I64d",&n); if(n == 1) { printf("1\n"); continue; } long long ans = (n*(n+1))%(20090524*6); ans = (ans*(n+2)/6)%20090524; printf("%lld\n",ans); } return 0; }
成功解决,ok。
- 数学知识 同余与模算术
- 同余与模算术
- 同余与模算术
- 同余与模算术
- 同余与模算术 与 poj1995
- 阶乘相关<同余与模算术>
- 同余模算术
- 同余 模算术 中国剩余定理
- 算法竞赛入门经典:第十章 数学概念与方法 10.3同余与模算术
- ZOJ3785 What day is that day?(同余与模算术)
- 例题10-1 UVa11582 Colossal Fibonacci Numbers!(同余与模算术)
- 模运算与同余
- 同余与模运算
- 同余与模运算
- 同余与模运算
- 同余与模运算
- 同余与取模
- 同余与模运算
- VT-x is not available (VERR_VMX_NO_VMX) 的解决方案
- IOS学习日志(Controller)1.5
- 【小白入门】如何快速下载到能够免费试用30天的Office 2016完整套装,秘钥版本
- 与linux上用户相关的一些命令finger|id|w|who|last|lastlog|write|mesg|wall
- 关于char强转为int(vc)
- 数学知识 同余与模算术
- python笔记02--SDK动态调用
- 【发现新课】贴出推荐基础感兴趣的链接(不定期补充)
- 灰背鸫
- Sublime Text 3
- maven仓库快速镜像
- Maven2 根据项目生成模版项目,并使用该模板批量创建工程
- maven3实战之仓库(镜像)
- python解决文本乱码问题及文本二进制读取后的处理