0% found this document useful (0 votes)
44 views5 pages

C++ Algorithms for Fast Power and Primes

The document contains four separate C++ programs that implement various algorithms. BÀI 01 calculates powers of two numbers using fast and slow exponentiation methods. BÀI 02 checks for 'sieu' numbers in an array, BÀI 03 counts specific pairs in an array based on their properties, and BÀI 04 implements a segment tree to find minimum values over a range with updates.

Uploaded by

hoaitan.nhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

C++ Algorithms for Fast Power and Primes

The document contains four separate C++ programs that implement various algorithms. BÀI 01 calculates powers of two numbers using fast and slow exponentiation methods. BÀI 02 checks for 'sieu' numbers in an array, BÀI 03 counts specific pairs in an array based on their properties, and BÀI 04 implements a segment tree to find minimum values over a range with updates.

Uploaded by

hoaitan.nhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BÀI 01

#include <bits/stdc++.h>
using namespace std;
long long a,b,n,m;
long long base1=1000000000;

long long fastpow(long long x, long long n){


long long res=1;
while (n>0){

if(n%2==1) res=res*x;
res=res%base1;
x*=x;
x=x%base1;
n=n/2;
}
return res;
}
long long fastpow1(long long x, long long n){
long long res=1;
while (n>0){

if(n%2==1) res=res*x;
x*=x;
n=n/2;
}
return res;
}
long long slowpow(int x, int n)
{
long long t=1;
for(int i=1; i<=n; i++)
{
t=t*x;
}
return t;
}
int main()
{ freopen("[Link]","r",stdin);
freopen("[Link]","w",stdout);
cin>>a>>b>>n>>m;

// cout<<fastpow1(a,n)%base1+fastpow1(b,m)%base1<<endl;
// cout<<slowpow(a,n)%base1+fastpow(b,m)%base1;
cout <<fastpow(a,n)+fastpow(b,m);
return 0;
}
BÀI 02
#include <bits/stdc++.h>
using namespace std;
long long a[1000005], n;
bool dd[1000005];

void nhap(){
cin>>n;
for(long i=1;i<=n;i++)
cin>>a[i];
}
void sang(){
for(long long i=2;i<=1000002;i++)
dd[i]=1;
for(long long i=2;i*i<=1000002;i++)
if(dd[i])
for(long long j=2;i*j<=1000002;j++)
dd[i*j]=0;

}
bool sieu(long long x){
int du;
while(x>0&&dd[x]){
x=x/10;
}
if(x==0)
return 1;
return 0;
}
int main()
{ int d=0;
freopen("[Link]","r",stdin);
freopen("[Link]","w",stdout);

nhap();
sang();
// cout<<sieu(32);
// for(int i=1;i<=100;i++)
// cout<<dd[i]<<" ";
for(long i=1;i<=n;i++)
if(sieu(a[i]))
d++;
cout<<d;
return 0;
}
BÀI 03
#include <bits/stdc++.h>

using namespace std;

map<int, bool> mapp;


vector<int> even, odd;
int n, ans, a[50006], max_ai;
void isrt(int val) {
bool if_exist = [Link](val);
if(if_exist == false)
[Link](make_pair(val, false));
return ;
}

int cnt1[100005];

void sub1() {
int res = 0;
for(int i = 1; i <= n; ++i)
for(int j = i+1; j <= n; ++j)
if((a[i] + a[j]) % 2 == 0)
for(int t = 1; t <= n; ++t)
if(a[t] == (a[i] + a[j]) / 2 && cnt1[t] == 0)
res++, cnt1[t] = 1;
cout << res;
}

int dem[100005];

void sub2() {
for(int i = 1; i <= n; ++i)
dem[a[i]]++;
int res = 0;
for(int i = 1; i <= n; ++i)
for(int j = i+1; j <= n; ++j)
if((a[i] + a[j]) % 2 == 0)
res += dem[(a[i] + a[j]) / 2], dem[(a[i] + a[j]) / 2] = 0;
cout << res;
}

void subtask3() {
sort(a + 1, a + n + 1);
for(int i = n; i > 0; i--) {
isrt(2 * a[i]);

if(a[i] % 2 == 0) {
for(int u : even)
if([Link](a[i] + u) == true && [Link](a[i] + u) == false)
ans++, [Link](a[i] + u) = true;
even.push_back(a[i]);
} else {
for(int u : odd)
if([Link](a[i] + u) == true && [Link](a[i] + u) == false)
ans++, [Link](a[i] + u) = true;
odd.push_back(a[i]);
}
}

cout << ans;


}

int cnt[5005];
bool chk[5005];

void subtask4() {
for(int i = 1; i <= n; i++)
cnt[a[i]]++;
for(int i = 1; i <= max_ai; i++) {
if(cnt[i] > 1)
chk[i] = true;
for(int j = i + 2; j <= max_ai; j += 2)
if(cnt[i] > 0 && cnt[j] > 0 && cnt[(i + j) / 2] > 0)
chk[(i + j) / 2] = true;
}

for(int i = 1; i <= max_ai; i++)


ans += cnt[i] * chk[i];

cout << ans;


}

int main() {
ios_base::sync_with_stdio(false);
[Link](0), [Link](0);

freopen("[Link]", "r", stdin);


freopen("[Link]", "w", stdout);

cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
max_ai = max(max_ai, a[i]);
}

if(n <= 3000 && max_ai <= 1000000000)


subtask3();
else if(n <= 50000)
subtask4();
}
BÀI 04
#include<bits/stdc++.h>
using namespace std;
int n, k, a[500005], st[2000005];
void update(int id, int l, int r, int pos, int val) {
if(pos < l || r < pos)
return;
if(l == r) {
st[id] = val;
return;
}
int mid = (l + r) / 2;
update(2 * id, l, mid, pos, val);
update(2 * id + 1, mid + 1, r, pos, val);
st[id] = min(st[2 * id], st[2 * id + 1]);
}
int get(int id, int l, int r, int u, int v) {
if(v < l || r < u)
return INT_MAX;
if(u <= l && r <= v)
return st[id];
int mid = (l + r) / 2;
int x = get(2 * id, l, mid, u, v);
int y = get(2 * id + 1, mid + 1, r, u, v);
return min(x, y);
}
int main() {
freopen("[Link]", "r", stdin);
freopen("[Link]", "w", stdout);
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= k; i++)
update(1, 1, n, i, a[i]);
for(int i = k + 1 ; i <= n; i++) {
int val = get(1, 1, n, i - k, i - 1) + a[i];
update(1, 1, n, i, val);
}
cout << get(1, 1, n, n - k + 1, n);
}

You might also like