Copy and Submit II

 

 这题内存只有512k,直接提交的话肯定错,所以需要给他优化一下,首先就要看懂代码是在干什么

比如说如果是输入3个数 a,b,c,就应该输出1+a+b+c+ab+ac+bc+abc,当时做的时候就推到这一步,然后就不知道怎么做了,回来问了一下才知道有公式

1+a+b+c+ab+ac+bc+abc=(1+a)(1+b)(1+c)

这个样子就很好办了,我的代码如下:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int M = 1000000007;
 6 
 7 int main()
 8 {
 9     int n,temp;
10     long long ans;
11     while (scanf("%d", &n)!=EOF)
12     {
13         ans = 1;
14         for (int i = 0; i < n; i++)
15         {
16             scanf("%d", &temp);
17             ans = (ans*(1 + temp) % M) % M;
18         }
19         printf("%ld\n", ans);
20     }
21     return 0;
22 }

猜你喜欢

转载自www.cnblogs.com/zl-nirvana/p/8921993.html
ii