Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题:
Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d;
首先让Daxia求出数列A(0)前n项和,得到新数列A(1);
然后让Daxia求出数列A(1)前n项和,得到新数列A(2);
接着让Daxia求出数列A(2)前n项和,得到新数列A(3);
...
最后让Daxia求出数列A(m-1)前n项和,得到新数列A(m);
测试包含多组数据,每组一行,包含四个正整数a(0<=a<=100),d(0<d<=100),m(0<m<=1000),i(1<=i<=1000000000).
每组数据输出一行整数,数列A(m)的第i项mod1000000007的值.
1 1 3 4
35
A(0): 1 2 3 4
A(1): 1 3 6 10
A(2): 1 4 10 20
A(3): 1 5 15 35
So the 4th of A(3) is 35.
找规律,确定第m,i项有几个a和d,然后排列组合计算一下就好了,因为m很小,直接循环就行。
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%lld%lld",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
typedef long long LL;
const int mod = 1e9 + 7;
LL a, d, n, m;
LL inv(LL x)
{
return x == 1 ? 1 : inv(mod%x)*(mod - mod / x) % mod;
}
LL C(LL x, LL y)
{
LL res = 1;
rep(i, 1, y)
{
(res *= (x - i + 1) % mod) %= mod;
(res *= inv(i)) %= mod;
}
return res;
}
int main()
{
while (intwo(a, d), intwo(m, n) != EOF)
{
printf("%lld\n", (C(n + m - 1, m)*a + C(n + m - 1, m + 1)*d) % mod);
}
return 0;
}