(递归转递推)Copy and Submit II

传送门

Description:

 
 
1
// Q.cpp
2
#include <iostream>
3
using namespace std;
4
const long long M = 1000000007;
5
const long long MAXL = 1000000;
6
long long a[MAXL];
7
long long Q(int n, long long t)
8
{
9
    if(n < 0) return t;
10
    return (Q(n - 1, t) + Q(n - 1, (t * a[n]) % M)) % M;
11
}
12
int main()
13
{
14
    int n;
15
    while(cin >> n)
16
    {
17
        for(int i = 0; i < n; ++i)
18
        {
19
            cin >> a[i];
20
            a[i] %= M;
21
        }
22
        cout << Q(n - 1, 1) << endl;
23
    }
24
    return 0;
25
}

Input:

Input consists of several test cases. Each test case begins with an integer n. Then it's followed by n integersa[i]

0<n<=1000000

0<=a[i]<=10000

There are 100 test cases at most. The size of input file is less than 48MB. 

Output:

Maybe you can just copy and submit. Maybe not. 

样例输入
1
233
1
666
样例输出
234
667
题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛

比赛时看到这个代码,没想到递归转成递推,一直想着是不是有什么规律,,,道行太浅,继续修炼!

对了,不要用cin,cout,即使解除了iostream与stdio输出流的绑定,还是超时。

#include<iostream>
#include<cstdio>
using namespace std;
const long long M=1000000007;
const long long MAXL=1000000;
long long a;

int main()
{
    int n;
    while(~scanf("%d",&n)){
        long long res=1;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a);
            res=(res+(res*a)%M)%M;
        }
        printf("%lld\n",res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值