Copy-and-Submit-II

题目链接 https://nanti.jisuanke.com/t/26220

Description:

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

Input:

Input consists of several test cases. Each test case begins with an integer n. Then it’s followed by n integers a[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
2
3
4
1
233
1
666

样例输出

1
2
234
667

题解

要求写一个与给定的程序功能一样的程序
观察这个递归,然后发现它是求(1+a1)(1+a2)(1+a3)..(1+an) ,递推的求就可以了
注意内存大小,不能开数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
typedef long long ll;
ll mod = 1000000007;

int main() {
ll n;
while(scanf("%lld",&n)!=EOF){
ll a=1,fac=1;
for (int i=1;i<=n;i++){
scanf("%lld",&a);
fac=(fac+fac*a%mod)%mod;
}
printf("%lld\n",fac);
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!