FZU Problem 2238 Daxia & Wzc's problem

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);

规律题,首先写出 a、a+d、a+2d、a+3d...这个容易写出

下面一行也容易写出:a、2a+d、3a+3d....

再下一行,确实难写,但是通过上面两行可以得出,dp[i][j] = dp[i-1][j] + dp[i][j-1]

然后可以顺利写出后面的,找到通项公式。

ans = C(m+i-1,m)*a + C(m+i-1,i-2)*d

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL a,d,m,i;
const int MOD = 1000000007;
LL quick_pow (LL a,LL b,LL MOD)
{
    //求解 a^b%MOD的值
    LL base=a%MOD;
    LL ans=1; //相乘,所以这里是1
    while (b)
    {
        if (b&1)
        {
            ans=(ans*base)%MOD; //如果这里是很大的数据,就要用quick_mul
        }
        base=(base*base)%MOD;    //notice
        //注意这里,每次的base是自己base倍
        b>>=1;
    }
    return ans;
}
LL C (LL n,LL m,LL MOD)
{
    if (n<m) return 0; //防止sb地在循环,在lucas的时候
    if (n==m) return 1;
    LL ans1 = 1;
    LL ans2 = 1;
    LL mx=max(n-m,m); //这个也是必要的。能约就约最大的那个
    LL mi=n-mx;
    for (int i = 1; i <= mi; ++i)
    {
        ans1 = ans1*(mx+i)%MOD;
        ans2 = ans2*i%MOD;
    }
    return (ans1*quick_pow(ans2,MOD-2,MOD)%MOD); //这里放到最后进行,不然会很慢
}

void work ()
{
    if (i==1)
    {
        printf ("%lld\n",a);
        return;
    }
    LL ans = (C(m+i-1,m,MOD)*a%MOD+C(m+i-1,i-2,MOD)*d%MOD)%MOD;
    printf ("%lld\n",ans);
    return ;
}
int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    while (scanf("%lld%lld%lld%lld",&a,&d,&m,&i)!=EOF) work();
    return 0;
}

FZU Problem 2238 Daxia & Wzc's problem

时间: 2024-08-05 15:08:22

FZU Problem 2238 Daxia & Wzc's problem的相关文章

Problem 2238 Daxia &amp; Wzc&#39;s problem 1627 瞬间移动

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 http://acm.fzu.edu.cn/problem.php?pid=2238 对应的51NOD这个题,先把n--和没m-- 再套公式 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorit

FOJ有奖月赛-2016年8月 Problem A Daxia &amp; Wzc&#39;s problem(找规律)

Problem A Daxia & Wzc's problem Accept: 42    Submit: 228Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题: Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d; 首先让Daxia求出数列A(0)前n项和,得到新数列A(1); 然后让Daxia求出数列A(

SPOJ Problem 1688:A Very Easy Problem!

递归题..但是没想到的是..这竟然是传说中的答案提交!!害我错了两遍.. 生成程序.. #include<cstdio> int i,a[6]={1315,73,136,255,1384,16385}; void search(int s){ int t=0,r=s; if (s==1)printf("2(0)"); if (s==2)printf("2"); if (s==3)printf("2+2(0)"); if (s>3

FOJ有奖月赛-2016年8月(daxia专场之过四题方有奖)

6/7 这里简单写个题解,由于源代码无法查看,所以过了的题目的代码就不贴了.... 题A Problem A Daxia & Wzc's problem 题意:略 题解:这个东西显然推公式,最后推得的一个组合数学的公式与第一项和公差有关(具体公式隔太久不记得了,囧).然后注意到这个m只有一千,虽然这个i很大,但是C(a,b)的运算次数显然是由a和b较小的一个决定,所以可以优化到算一个组合数的复杂度是min(m,i). 题B Problem B Daxia & Yayamao's probl

FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)

Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shortest closed path or circuit that visits every edge of a (connected) undirected graph. When the graph has an Eulerian Circuit (a closed walk that cover

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

Problem A: Artificial Intelligence?

Description Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first! So they don't state a problem like ``U=10V, I=5A, P=?"

POJ 3468 A Simple Problem with Integers(线段树)

题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 56005   Accepted: 16903 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with