[Mảng 1 Chiều Cơ Bản]. Bài 13. Tính tổng và tích các phần tử

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Java 4.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, hãy tính tổng, tích của các phần tử trong mảng và lấy dư với 10^9+7.


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

1<=N<=10^6

0<=A[i]<=10^6


Đầu ra

Dòng đầu tiên in ra tổng các phần tử trong mảng chia dư với 10^9 + 7

Dòng thứ hai in ra tích các phần tử trong mảng chia dư với 10^9 + 7;


Ví dụ :

Input 01
6
997893 995053 997553 996212 998316 992144
Output 01
5977171
436766709

Comments

Please read the guidelines before commenting.



  • 0
    nhan17122012  commented on July 9, 2026, 11:17 a.m.

    /*#include<bits/stdc++.h>

    include<bits/stdc++.h>

    using namespace std; const long long mod=1e9+7;

    long long a[1000006],n,x;

    long long tong1=0,tong2=0,dem1=0,dem2=1;

    int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    tong2=1;
    for(int i=1; i<=n; i++)
    {
    
        cin>>x;
        tong1=(tong1+x)%mod;
    
        tong2=(tong2*x)%mod;
    }
    
    cout<&lt;tong1<<'\n'<<tong2;
    

    }


  • 0
    Kelvin2206  commented on April 4, 2026, 3:49 a.m.

    include <iostream>

    #include <vector>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <iomanip>
    #include <numeric>
    #include <utility>
    using namespace std;
    
    typedef long long ll;
    typedef long double ld;
    typedef string str;
    typedef vector<ll> vi;
    typedef pair&lt;ll, ll> pi;
    
    #define pb push_back
    #define rs resize
    #define all(x) x.begin(), x.end()
    #define el '\n'
    
    const ll INF = 1e18;
    const ll MOD = 1e9 + 7;
    
    vi a;
    
    void Solve() {
        ll n; cin >> n;
        a.rs(n); for (auto& x : a) cin >> x;
    
        ll sum = 0, product = 1;
    
        for (ll i = 0; i < n; ++i) {
            sum = (sum + a[i]) % MOD;
            product = (product * a[i]) % MOD;
        }
    
        cout << sum << el << product;
    }
    
    int main() {
        ios::sync_with_stdio(0); cin.tie(nullptr);
    
        ll t=1; //cin >> t;
        while (t--) Solve();
    }
    

  • 0
    kietne  commented on Dec. 13, 2025, 12:27 a.m.

    FULL AC NHA MẤY BẠN

    include <bits/stdc++.h>

    using namespace std; using ll = long long; const int DONE = 1e9+7;

    int main() { ios::syncwithstdio(false); cin.tie(nullptr);

    int n; 
    cin >> n;
    
    ll tong = 0, tich = 1;
    ll x;
    for (int i=0; i < n; i++) {
        cin >> x;
        tong = (tong + x) % DONE;
        tich = (tich * x) % DONE;
    }
    
    cout << tong << "\n" << tich << "\n";
    return 0;
    

    }


  • -1
    ThG  commented on Oct. 3, 2025, 4:02 p.m.

    Code AC

    #include <bits/stdc++.h>
    using namespace std;
    const long long MOD = 1e9 + 7;
    long long n;
    vector &lt;long long> a;
    long long s = 0, tich = 1;
    void nhap() {
        cin >> n;
        a.resize(n + 1);
        for (int i = 1; i <= n; i++){
            cin >> a[i];
            long long x = a[i];
            s = (s + x) % MOD;
            tich = (tich * x)% MOD;
        }
        cout << s << '\n' << tich;
    }
    void solve() {
    
    }
    #define task ""
    signed main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        if (fopen(task".inp", "r")) {
            freopen(task".inp", "r", stdin);
            freopen(task".out", "w", stdout);
        }
        nhap();
        solve();
        return 0;
    }
    

  • 0
    naipret  commented on Sept. 18, 2025, 6:00 a.m. edit 5
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    constexpr long long kModulus = static_cast<int>(1e9 + 7);
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      long long sum = 0, product = 1;
      for (int ele : vec) {
        sum += static_cast < long long>(ele) % kModulus;
        sum %= kModulus;
    
        product *= static_cast < long long>(ele) % kModulus;
        product %= kModulus;
      }
    
      cout << sum << '\n';
      cout << product;
    }
    

  • -1
    Nguyen0907  commented on Aug. 15, 2025, 9:05 p.m.

    FULL AC

    #include <bits/stdc++.h>
    

    using namespace std;

    define fastIO() ios::syncwithstdio(false); cin.tie(nullptr);

    define ll long long

    int main() { fastIO();

    ll mod = 1e9 + 7;
    
    int n;
    cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    
    int sum = 0; ll tich = 1;
    for(int i = 0; i < n; i++) {
        sum += A[i] % mod;
        sum %= mod;
    
        tich *= A[i] % mod;
        tich %= mod;
    }
    
    cout << sum << "\n" << tich << endl;
    
    return 0;
    

    }


  • -10
    KHOA87  commented on April 13, 2025, 8:24 a.m. edit 3

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    KHOA87  commented on April 13, 2025, 8:22 a.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 0
    masterD  commented on March 13, 2025, 8:55 a.m.

    .


  • 0
    Dungx_2008  commented on Nov. 24, 2024, 10:16 a.m.

    include <bits/stdc++.h>

    define MOD 1000000007

    define ll long long

    define N 10000000

    using namespace std; ll n,a[N],sum=0,tich=1; int main() { cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; for(int i=1;i<=n;i++) { sum = (sum%MOD + a[i]%MOD)%MOD; tich = (tich%MOD*a[i]%MOD)%MOD; } cout << sum << '\n'; cout << tich; return 0; }


  • -5
    HungDSQ2  commented on Nov. 4, 2024, 9:50 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 0
      id_07  commented on July 20, 2025, 7:48 a.m.

      chat gpt af


  • -1
    khanhhduy_1305  commented on Sept. 17, 2024, 4:09 a.m.

    include<bits/stdc++.h>

    using namespace std; int main() { long long n,tong=0,tich=1,a[100002]; cin>>n; for(long long i=0;i<n;i++) { cin>>a[i]; tong+=a[i]%long(1e9+7); } for(long long i=0;i<n;i++) { a[i]=a[i]%long(1e9+7); tich=(1LLtich%long(1e9+7)(1LL*a[i]%long(1e9+7)))%long(1e9+7); } cout<<tong<