Hàm, LTS TG Hòag

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

HÀM,LTSO

CHỮA BÀI:TRỢ GIẢG HÒAG


 PHI HÀM EULER
Đếm số lượng các số nguyên tố cùng nhau với n
không vượt quá n.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long gcd(long long x, long long y){


if(y==0) return x;
return gcd(y,x%y);
}

#define ll long long

ll phi(ll n){
ll res = n;
for(int i = 2; (long long)i * i <= n; i++){
if(n % i == 0){
while(n % i == 0) n /= i;
res -= res / i;
}
}
if(n != 1){
res -= res / n;
}
return res;
}
int main() {
long long a;
cin >> a;
cout << phi(a);
return 0;
}

Thừa số nguyên tố thứ K


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int kth_prime(int n, int k) {


int cnt=0;
for(int i=2; i<=sqrt(n); i++) {
if(n%i==0) {
while(n%i==0) {
n/=i;
cnt++;
if(cnt==k) return i;
}
}
}
if(n>1) {
cnt++;
if(cnt==k) return n;
}
return -1; // da phan tich xong nhung khong tim duoc
}

int main() {
int n, k;
cin >> n >> k;
cout << kth_prime(n, k);
return 0;
}

LKE CSO NGTO


Liệt kê các chữ số nguyên tố của n cùng số lần xuất hiện,
theo thứ tự từ bé đến lớn. Sau đó cách ra 1 dòng và liệt
kê các chữ số nguyên tố của n cùng số lần xuất hiện,
nhưng theo thứ tự xuất hiện trong n.
Phươg pháp:sử dụng mảg đánh dấu.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

//mang danh dau


int d[10]; //so lan xuat hien cua i
void dem(long long n){
long long m=n;
while(m){ //duyet cac cso của m.
d[m%10]++; //duyet qua từg cso của n
m/=10;
}
for(int i=0;i<10;i++){ //duyet cac cso của n
if(d[i] !=0 && (i==2 || i==3 || i==5 || i==7)) //i co xh
thi moi in
cout << i << ' ' << d[i] << "\n";
}
cout << endl;
//lat nguoc n
m=0;
while(n){
m=m*10+n%10; //lật ngược n
n/=10; //duyệt các số từ trái qua phải.
}
//duyet m
while(m){
int x=m%10;
if(d[x] !=0 && (x==2 || x==3 || x==5 || x==7)) //x
co xh thi moi in
{
cout << x << ' ' << d[x] << "\n";
d[x]=0; //reset lai dx lsau gap lai k cout
nua. }
m/=10;
}
}
int main() {
long long n; cin >> n;
dem(n);
return 0;
}

Số nguyên dương nhỏ nhất chia hết cho x, y,


z.
#include <iostream>
#include <algorithm>
using namespace std;
long long gcd(long long a, long long b) {
while (b != 0) {
long long temp = b;
b = a % b;
a = temp;
}
return a;
}

long long lcm(long long a, long long b) {


return (a / gcd(a, b)) * b;
}

int countDigits(long long n) {


int cnt = 0;
while (n) {
cnt++;
n /= 10;
}
return cnt;
}
Cạch 1:
int main() {
long long x, y, z, n;
cin >> x >> y >> z >> n;

long long t = lcm(x, lcm(y, z)); //bcnn của 3 số.


//đáp án cần tìm là bội của t có n chữ số.

long long res = t;


while (countDigits(res) < n) {
res += t;
}

if (countDigits(res) == n)
cout << res;
else
cout << -1;

return 0;
}
Cach 2: chia lay nguyên
Int main(){
Int a,b,c,n;
Cin >> a >> b >> c >> n;
Long long t=lcm(a,lcm(b,c));
Long long l=pow(10,n-1),u=pow(10,n); //tìm số min trog
đoạn [l,u-1] mà chia hết cho t.
Long long y=l/t; //chia lấy nguyên
If(l%x) y++; //nếu phép chia có dư -> tăg y lên 1 đơn vị -
> khi đó t*y > l;
Long long ans=y*t;
If(ans<u) cout << ans; //ktra ans có nhỏ hơn u ko?
Else cout << -1;
}
UCLN,BCNN
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long long gcd(long long a,long long b){
while(b!=0){
Long long r=a%b;
a=b; b=r; } return a;
}
long long lcm(long long a, long long b){
return a/gcd(a,b)*b;
}
int main() {
long long a,b; cin >> a >> b;
cout << gcd(a,b) << ' ' << lcm(a,b);
return 0;
}

Note : gcd dạng đệ quy


Long long gcd(long long a, long long b) {
If(b==0) return a;
Return gcd(b,a%b);

LŨY THỪA Tính a^b%1000000007 với a,b nguyên không


âm.
Sd: Lũy thừa nhị phân.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod=1e9+7;
long long pow_mod(long long a,long long b){
a%=mod; //giam a
long long res=1;
while(b) {
if(b%2) res = (res*a)%mod; //nếu b lẻ.
a =(a*a)%mod;
b/=2;
}
return res;
}
int main() {
long long a,b;
cin >> a >> b;
cout << pow_mod(a,b);
return 0;
}
SỐ LỰOG ƯỚC CỦA N!
(đếm sl ước của n!)
Sd mảg đánh dấu+ phân tích từg tso.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod=1e9+7;

int d[100005]; //d[i] smu cua tso ngto i.


void ptich(long long n){
for(int i=2;i<=sqrt(n);i++){
if(n%i==0){
while(n%i==0){
d[i]++;
n/=i;
}
}
}
if(n>1) d[n]++;
} //cứ gặp 2 là tăg biến đếm lên.
int main() {
long long x; cin >> x;
for(int i=2;i<=x;i++){
ptich(i);
}
long long res=1;
for(int i=2;i<=100005;i++){
if(d[i]!=0) res=(res*(d[i]+1))%mod;
}
cout << res;
return 0;
}

TÍNH GIÁ TRỊ CỦA HÀM F


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long sum(long long dau, long long cuoi,long long


khoangcach){
//tinh so so hạng=(cuoi-dau)/khoangcach+1;
long long cnt=(cuoi-dau)/khoachcach +1;
//tong day so=(cuoi+dau)*(cnt)/2
return (cuoi+dau)*cnt/2;
}
int main() {
long long n; cin >> n;
//sum chan, sum le -> sum chan-sum le
long long chan_cuoi,le_cuoi;
if(n%2 !=0){
chan_cuoi=n-1;
le_cuoi=n;
}
else {
chan_cuoi=n;
le_cuoi=n-1;
}
cout << sum(2,chan_cuoi,2)-sum(1,le_cuoi,2);
// cout << (chan_cuoi +2)*((chan_cuoi-2)/2+1)/2 -
(le_cuoi+1)*((le_cuoi-1)/2+1)/2;
return 0;
}

CHỮ SỐ CUỐI CÙNG


Cho n, in ra chữ số cuối cùng của 1378^n.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
long long t=1;
while(t--){
long long n; cin >> n;
if(n%4==0) cout << 6;
else if(n%4==1) cout << 8;
else if(n%4==2) cout << 4;
else cout << 2;
}
return 0;
}

You might also like