[Mảng 1 Chiều Cơ Bản]. Bài 10. Cân bằng nguyên tố

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.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 liệt kê các chỉ số i trong mảng thỏa mãn : Tổng các phần tử bên trái i và tổng các phần tử bên phải i là các số nguyên tố

Gợi ý :

Đối với mỗi chỉ số i xây dựng 1 vòng for duyệt từ 0 tới i - 1 để tính tổng các số bên trái của i, 1 vòng for duyệt từ i + 1 tới N - 1 để tính tổng các phần tử nằm bên phải của i => Kiểm tra cả 2 tổng là số nguyên tố thì in ra chỉ số i, chú ý reset biến tổng trái và phải tại mỗi vòng lặp.


Đầ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<=1000

  • -10^3<=A[i]<=10^3


Đầu ra
  • In ra các chỉ số thỏa mãn trên một dòng, các số cách nhau một khoảng trắng

Ví dụ :

Input 01
5
53 5 69 47 19
Output 01
3

Comments

Please read the guidelines before commenting.



  • 0
    cjzchhh  commented on Sept. 6, 2026, 3:02 p.m.

    include <bits/stdc++.h>

    using namespace std; int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); int arr[1000]; int n; cin >> n; for(int a = 0; a < n; a++){ cin >> arr[a]; } for(int i = 0; i < n; i++){ int tongtrai = 0, tongphai = 0; bool yes1 = true, yes2 = true; for(int j = 0; j < i; j++){ tongtrai += arr[j]; } if(tongtrai < 2){ yes1 = false; } for(int y = 2; y * y <= tongtrai; y++){ if(tongtrai % y == 0){ yes1 = false; break; } } for(int x = i + 1; x < n; x++){ tongphai += arr[x]; } if(tongphai < 2){ yes2 = false; } for(int z = 2; z * z <= tongphai; z++){ if(tongphai % z == 0){ yes2 = false; break; } } if(yes1 && yes2){ cout << i << " " << '\n'; } } return 0; }xin loi vi da ko xài hàm :))))


  • 0
    yannsommer_2012  commented on Sept. 1, 2026, 2:53 p.m.

    include <bits/stdc++.h>

    using namespace std;

    long long dp[1000005], a[1000005], n;

    bool nt(long long x) { if (x < 2) return false; for (long long i = 2; i<= sqrt(x); i++) { if (x % i == 0) return false; } return true; } int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1] + a[i]; } long long r = dp[n]; long long l = 0; for (int i = 1; i <= n; i++) { r -= a[i]; if (nt(l) && nt(r)) { cout << i - 1 << " "; }

        l += a[i]; 
    }
    return 0;
    

    }


  • 0
    hungoanhlinh  commented on July 7, 2026, 1:04 p.m.

    bai em sai o dau a


  • 0
    hungoanhlinh  commented on July 7, 2026, 1:04 p.m.

    include <bits/stdc++.h>

    using namespace std;

    bool snt(int n){ if(n < 2)return false; for(int k = 2; k < sqrt(n); k++){ if(n % k == 0)return false; } return true; }

    int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); int n; cin >> n; int a[1003]; for(int i = 0; i < n; i++){ cin >> a[i]; } for(int i = 0; i < n; i++){ int dem1 = 0,dem2 = 0; for(int j = 0; j < i; j++){ dem1 += a[j]; } for(int k = i + 1; k < n; k++){ dem2 += a[k]; } if(snt(dem1) && snt(dem2)){ cout << i << " "; } } return 0; }


  • -2
    minhduc2803  commented on June 19, 2026, 2:47 p.m.

    ý tưởng cho ae nào bí : FULL AC: tôi dùng hàm kết hợp duyệt trâu: đầu tiên ae tạo 1 hàm kiểm tra số nguyên tố(cái này tự làm nhé) sau đó trong main ae tạo 1 vòng lặp i trong vòng lặp i ae tạo 2 biến tongtrai và phải đặt bằng 0 nhé, ae tạo thêm 1 vòng lặp con từ 0 tới i-1, trong vòng lặp con này ae dùng phần tử của vòng lặp con này để cộng dồn vào tongtrai nhé, sau đó ae tạo 1 vòng lặp con nữa từ i+1 tới n-1, ae lấy phần tử của vòng lặp này để cộng dồn vào tongphai nhé, rồi ra khỏi ngoặc của vòng lặp con ae kiểm tra tongtrai và phải có phải số nguyên tố ko, ae xài toán tử n&&hé, rồi ở cout ra i nhớ thêm khoảng trắng để ko mất điểm oan nha ae.


  • -1
    Jhanh  commented on Nov. 10, 2025, 9:10 a.m.
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    bool snt(int n){
        if (n < 2)  return false;
        for (int i=2; i*i<=n; i++){
            if (n%i == 0)  return false;
        }
        return true;
    }
    
    int main(){
        int n; cin >> n;
        int a[n];
        int right = 0;
        for (int i=0; i < n; i++){
            cin >> a[i];
            right += a[i];
        }
    
        right -= a[0];
        for (int i=1; i < n-1; i++){
            right -= a[i];
            if (snt(a[i-1]) && snt(right))  cout << i << ' ';
            a[i] += a[i-1];
        }
    
        return 0;
    }
    

  • -1
    thanhtrung1611  commented on Oct. 16, 2025, 10:46 a.m.

    include <iostream>

    include <math.h>

    using namespace std;

    bool PrimeNum(int x){ if (x < 2) return false; for(int i = 2; i <= sqrt(x); i++){ if( x % i == 0) return false; } return true; }

    int main(){ int a; cin >> a; int arr[a]; for(int i = 0; i < a; i++){ cin >> arr[i]; } for(int i = 0; i < a; i++){ int count1 = 0; int count2 = 0; for(int y = 0; y < i; y++){ count1 += arr[y]; } for(int z = i + 1; z < a; z++){ count2 += arr[z]; } if (PrimeNum(count1) && PrimeNum(count2)) cout << i << endl; } return 0; }


  • 0
    naipret  commented on Sept. 15, 2025, 3:16 p.m. ← edit 3
    #include <cmath>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    bool IsPrime(int val) {
      if (val < 2) {
        return false;
      }
    
      int loop_limit = static_cast<int>(sqrt(val));
      for (int seq_val = 2; seq_val <= loop_limit; seq_val++) {
        if (val % seq_val == 0) {
          return false;
        }
      }
    
      return true;
    }
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      for (int middle_idx = 0; middle_idx < num; middle_idx++) {
        int left_sum = 0, right_sum = 0;
        for (int left_idx = 0; left_idx < middle_idx; left_idx++) {
          left_sum += vec[left_idx];
        }
        for (int right_idx = middle_idx + 1; right_idx < num; right_idx++) {
          right_sum += vec[right_idx];
        }
        if (IsPrime(left_sum) && IsPrime(right_sum)) {
          cout << middle_idx << ' ';
        }
      }
    }
    

  • 0
    GIANG_BAO_NGOC  commented on Aug. 24, 2025, 2:34 a.m.

    from math import * def nt(n): if n < 2: return False for i in range(2, isqrt(n) + 1): if n % i == 0: return False return True

    n = int(input()) a = list(map(int, input().split())) for i in range(n): sumleft = 0 for j in range(0, i): sumleft += a[j]

    sum_right = 0
    for j in range(i + 1, n):
        sum_right += a[j]
    
    if nt(sum_left) and nt(sum_right):
        print(i)
    

  • -13
    thanhnay29  commented on Feb. 13, 2025, 7:58 a.m.

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


  • 4
    VDev  commented on Jan. 18, 2025, 1:47 p.m.

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll a[10000011];
    ll cnt = 0, s = 0, mark[10000011], trai = 0, phai = 0;
    bool nt(ll n){
        if(n < 2){
            return false;
        }else
        for(ll i = 2; i <= sqrt(n); i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
    int main(){
        ll n;
        cin >> n;
        for(ll i = 0; i < n; i++){
            cin >> a[i];
        }
        for(ll i = 0; i < n; i++){
                trai = 0, phai = 0;
                for(ll j = 0; j < i; j++){
                    trai += a[j];
                }
                for(ll h = i + 1; h < n; h++){
                    phai += a[h];
                }
                if(nt(trai) && nt(phai)){
                    cout << i << " ";
                }
        }
        return 0;
    }
    

  • -1
    mango_K  commented on Dec. 5, 2024, 7:47 a.m.

    include<bits/stdc++.h>

    using namespace std; bool snt(int n) { if(n<=1) return 0; for(int i=2;i<=sqrt(n);i++){ if(n%i==0){ return 0; } } return 1; } int main(){ int n,s=0; cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; s+=a[i]; } for(int i=0;i<n;i++){ int tt=0; int tp=0; for(int j=0;j<i;j++){ s+=a[j]; } for(int k=i+1;k<n;k++){ s+=a[k]; } if(snt(tp)&&snt(tt)) cout<<i<<" "; } return 0; } sai ở đâu vậy??


  • 0
    khiem101701  commented on Dec. 3, 2024, 10:25 a.m.

    include <iostream>

    include <math.h>

    using namespace std;

    bool ktsnt(int x){ if(x <= 1) return false; else{ for (int i = 2;i <= sqrt(x);i++){ if(x % i == 0){ return false; } } } return true; }

    int main(){ int n; cin>>n; int arr[n]; for (int i = 0;i < n;i++){ cin>>arr[i]; } for(int i = 1;i < n-1;i++){ int tongtrai = 0; int tongphai = 0; for(int j = 0;j < i;j++){ tongtrai += arr[j]; } for(int j = i+1;j < n;j++){ tongphai += arr[j]; } if(ktsnt(tongtrai) && ktsnt(tongphai)){ cout<<i<<" "; } } return 0; }


  • -1
    Chu_manh_thang_cpp  commented on Nov. 30, 2024, 9:04 a.m.

    def snt(n): kt = True if n < 2: kt = False else: for i in range(2, int(n ** 0.5) + 1): if n % i == 0: kt = False break return kt n = int(input()) a = list(map(int,input().split())) for i in range(n): s1 = 0 s2 = 0 for j1 in range(i): s1 += a[j1] for j2 in range(i + 1, n): s2 += a[j2] if snt(s1) and snt(s2): print(i,end= ' ')


  • -1
    Dungx_2008  commented on Nov. 25, 2024, 3:10 p.m.

    include <bits/stdc++.h>

    define ll long long

    define name ""

    define N 100000

    using namespace std; int pre[N],a[N],n; bool prime(int n) { if(n<2) return false; for(int i=2;i<=sqrt(n);i++) { if(n%i == 0) return false; } return true; } int main() { iosbase::syncwith_stdio(0); cin.tie(0);cout.tie(0); if(fopen(name".INP","r")) { freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; pre[0] = 0; pre[1] = a[1]; for(int i=2;i<=n;i++) pre[i] = pre[i-1] + a[i]; for(int i=2;i<n;i++) { if(prime(pre[i-1] - pre[0]) && prime(pre[n] - pre[i])) cout << i - 1 << " "; } return 0; }


    • -1
      Dungx_2008  commented on Nov. 25, 2024, 3:11 p.m.

      include <bits/stdc++.h>

      define N 100000

      using namespace std; int pre[N],a[N],n; bool prime(int n) { if(n<2) return false; for(int i=2;i<=sqrt(n);i++) { if(n%i == 0) return false; } return true; } int main() { cin >> n; for(int i=1;i<=n;i++) cin >> a[i]; pre[0] = 0; pre[1] = a[1]; for(int i=2;i<=n;i++) pre[i] = pre[i-1] + a[i]; for(int i=2;i<n;i++) { if(prime(pre[i-1] - pre[0]) && prime(pre[n] - pre[i])) cout << i - 1 << " "; } return 0; }


  • -2
    duongnc_pt  commented on Oct. 30, 2024, 3:48 p.m. ← edit 2

    Code cho ai cần nhé

    https://ideone.com/CQpDhx

    Ac cho mình xin ^


  • -1
    Hien_648  commented on Oct. 20, 2024, 12:53 a.m.

    Ai full test cho xin code với ạ. Cảm ơn nhiều


  • 1
    Thanhall201  commented on Aug. 4, 2024, 3:56 p.m.

    Vãi thật ae ạ! Bài này nó bắt các tổng trái và tổng phải của i đồng thời là số nguyên tố thì ta bỏ qua cả a[0] và a[n-1] vì bên trái a[0] là 0 kh phải snt bên phải a[n-1] là 0 cũng không phải là số nguyên tố. Tui mất cả tiếng để ktra đi ktra lại cái nộp thử nó lại đúng hết test =))


    • -3
      Nguyen_Hoang_Dat08  commented on Sept. 9, 2024, 7:47 a.m.

      vãi thanh niên này lm biếng thấy bà cố


  • 0
    hackerlo2803  commented on June 30, 2024, 7:45 p.m.

    include <bits/stdc++.h>

    using namespace std;

    int is_nt[1000001];

    void sang(){ memset(isnt, 1, sizeof(isnt)); isnt[0] = isnt[1] = 0; for (int i = 2 ; i < sqrt(1000000) ; i++){ for (int j = i*i ; j < 1000000 ; j+=i){ is_nt[j] = 0; } } }

    int main() { // iosbase::syncwith_stdio(false); // cin.tie(NULL); // cout.tie(NULL);

    sang();
    
    int n; cin >> n;
    
    int a[n];
    int sum = 0;
    for (int i = 0 ; i < n ; i++) {
        cin >> a[i];
        sum += a[i];
    }
    
    for (int i = 0 ; i < n ; i++){
        int left = 0;
        int right = 0;
        for (int t = 0 ; t <= i - 1 ; t++){
            left += a[t];
        }
        for (int p = n - 1 ; p >= i + 1 ; p--){
            right += a[p];
        }
        // cout << i << ". " << left << " " << right << "\n";
        if (is_nt[right] && is_nt[left] && right > 0 && left > 0) {
            cout << i;
            break;
        }
    }
    
    return 0;
    

    } sai ở đâu ạ


    • -2
      tranminhnhat_2k9  commented on July 16, 2024, 10:22 a.m.

      tại bạn chx cách số một khoảng trắng sau khi in ra cout <<i<<" ";


  • 1
    longhk2012  commented on June 2, 2024, 1:46 p.m.

    sao full test dc vay mn mong mn giup


    • 0
      hailuacx  commented on June 6, 2024, 3:03 a.m.

      Dùng mảng cộng dồn bạn nhé!