
一道c语言编程题,有额外加分哦
Theproblemissimple:Giveyoutwointegersnandk,yourtaskistocalculatetheresultof"(n1+n2+n3...
The problem is simple:
Give you two integers n and k, your task is to calculate the result of "(n1+n2+n3+...+nk ) % 1000000007". //如何处理这里数据溢出的情况
Input
There are multiple cases.Each case contains two integers n and k (0 ≤ n ≤ 108, 0 < k ≤ 106).
Output
For each test case, output the answer in a single line.
Sample Input
2 2
4 3
Sample Output
6
84 展开
Give you two integers n and k, your task is to calculate the result of "(n1+n2+n3+...+nk ) % 1000000007". //如何处理这里数据溢出的情况
Input
There are multiple cases.Each case contains two integers n and k (0 ≤ n ≤ 108, 0 < k ≤ 106).
Output
For each test case, output the answer in a single line.
Sample Input
2 2
4 3
Sample Output
6
84 展开
6个回答
展开全部
这个..给你个公式吧:
(a+b)%c=(a%c+b%c)%c
这下就不会溢出了,int就能搞定了。
如果还不懂,请追问。
(a+b)%c=(a%c+b%c)%c
这下就不会溢出了,int就能搞定了。
如果还不懂,请追问。
展开全部
哪里会溢出。
1. 现在一般unsigned int 是32位吧,足以表示 1000000007。即使你机器上int是16位,那用unsigned long就是32位。
2. 如果是前面那个和溢出,由于是求模,当累加到 1000000007时就清零好了,然后继续累加。
1. 现在一般unsigned int 是32位吧,足以表示 1000000007。即使你机器上int是16位,那用unsigned long就是32位。
2. 如果是前面那个和溢出,由于是求模,当累加到 1000000007时就清零好了,然后继续累加。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用long的数据类型 就可以了 至于是 signed 还是unsigned就随便你咯、
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将数据类型定义为long long................
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询