Bài đăng

Cách đọc file input và write file input - Java

Tạo file input.txt. Sau khi chạy xong code nó sẽ tự động sinh ra file output 5 1 2 2 3 3 4 4 5 5 6 Và dưới đây là code: import java . io . FileWriter ; import java . io . File ; import java . util . Scanner ; import java . math . BigInteger ; public class nhap { public static void main ( String [] args ){ try { /* Input*/ File myObj = new File ( "input.txt" ); Scanner myReader = new Scanner ( myObj ); Integer testcase = myReader . nextInt (); /*Output */ FileWriter myobj_out = new FileWriter ( "output1.txt" ); // Integer ans = 5; // Integer ans1 = 6; // myobj_out.write(ans.toString()); // myobj_out.write('\n'); // myobj_out.write(ans1.toString()); /*Solve */ for ( Integer i = 0 ; i < testcase ; i ++) { // String data = myReader.nextLine(); ...

Cách đa năng hoá toán tử < , và những lợi ích của nó trong set,map.

 Đề bài: https://codeforces.com/problemset/problem/608/A Code: #include < bits / stdc ++. h > using namespace std ; #define ll long long struct cap { ll tang , tg ; }; // bool tmp(cap a, cap b){ // if(a.tang==b.tang) return a.tg<b.tg; // else return a.tang>b.tang; // } bool operator < ( const cap & a , const cap & b ){ if ( a . tang == b . tang ) return a . tg < b . tg ; return a . tang > b . tang ; } int main (){ // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll n , s ; set <cap> ss ; cin >> n >> s ; cap arr [ n ]; ll i ; for ( i = 0 ; i < n ; i ++){ cin >> arr [ i ]. tang >> arr [ i ]. tg ; ss . insert ( arr [ i ]); } // for(auto pp: ss){ // cout<<pp.tang<<" "<<pp.tg<<'\n'; // } sort ( arr , arr + n ); ...

Priority Queue - Xuoi - Nguoc

 Nguồn: https://codeforces.com/problemset/problem/218/B Solve: //priority_queue <int> ; less //priority_queue <int,vector<int>,greater<int> > ; greater #include < bits / stdc ++. h > using namespace std ; #define ll long long priority_queue <ll> q_max ; priority_queue < ll , vector <ll> , greater <ll> > q_min ; int main (){ ll n , m ; cin >> n >> m ; ll i ; ll a [ m ]; for ( i = 0 ; i < m ; i ++){ cin >> a [ i ]; q_max . push ( a [ i ]); q_min . push ( a [ i ]); } ll res_max = 0 , res_min = 0 ; for ( i = 0 ; i < n ; i ++){ ll q ; ll p ; q = q_max . top (); p = q_min . top (); q_max . pop (); q_min . pop (); if ( q > 0 && p > 0 ){ res_max = res_max + q ; res_min = res_min + p ; q --; p --; if ( q > 0 ) q_max . push ( q ); if ( p > 0 ) q_min . push ( p...

Tài liệu machine learning

 1)  https://drive.google.com/file/d/1lNjzISABdoc7SRq8tg-xkCRRZRABPCKi/view?fbclid=IwAR0VR3Ewkf_4OY7G5m41RYztwlvmeJfJuDBa_Z25gNBXJKJy1kheVD3t6Dk

Check nguyên tố bằng Java

import java.util.*;  import java.math.*;     class Main  {      //Function to check and return prime numbers      static boolean checkPrime(String n)      {          // Converting long to BigInteger          BigInteger b = new BigInteger(n);             return b.isProbablePrime(1);      }         // Driver method      public static void main (String[] args)                           throws java.lang.Exception      {         String n = "1000000000000000000";         System.out.println(checkPrime(n));      }  } Link: https://www.onlinegdb.com/

Cách tính a*b mod m , a^b mod m , a^(-1) mod m với m không phải là số nguyên tố (inversion có nghĩa khi (a,m)=1)

#include <bits/stdc++.h> using namespace std ; #define ll long long #define MOD (ll)(1000000000000000009) ll inv ( ll a , ll mod ){ ll r = mod ; ll nr = a ; ll t = 0 ; ll nt = 1 ; ll tmp ; while ( nr != 0 ){ ll q = r / nr ; tmp = nt ; nt = t - q * nt ; t = tmp ; tmp = nr ; nr = r - q * nr ; r = tmp ; } if ( r > 1 ) return - 1 ; // no inverse if ( t < 0 ) t += mod ; return t ; } ll f ( ll a , ll n , ll mod ){ ll res = a , ans = 0 ; while ( n ){ if ( n % 2 ) ans = ( ans + res ) % mod ; res = ( res + res ) % mod ; n /= 2 ; } return ans ; } ll po ( ll a , ll n , ll mod ){ ll res = a , ans = 1 ; while ( n ){ if ( n % 2 ) ans = f ( ans , res , mod ); res = f ( res , res , mod ); n /= 2 ; } return ans ; } int main (){ ll x , y , z , m , dem = 0 , res1 , res2 , res , tmp ...

DUT - Train

https://docs.google.com/spreadsheets/d/154ndCLFTvT_dS5o_Dh2ikAJwd4fMo8xC9roMc9q_RAU/edit?usp=sharing

Sàng nuyên tố - Vinoy

void sieve ( int N ) { bool isPrime [ N + 1 ]; for ( int i = 0 ; i <= N ; ++ i ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; ++ i ) { if ( isPrime [ i ] == true ) { // Mark all the multiples of i as composite numbers for ( int j = i * i ; j <= N ; j += i ) isPrime [ j ] = false ; } } }

Python - Liệt kê tất các các chu trình trong đồ thị

graph = [[1, 2], [1, 3], [1, 4], [2, 3], [3, 4], [2, 6], [4, 6], [8, 7], [8, 9], [9, 7]] cycles = [] def main(): global graph global cycles for edge in graph: for node in edge: findNewCycles([node]) for cy in cycles: path = [str(node) for node in cy] s = ",".join(path) print(s) def findNewCycles(path): start_node = path[0] next_node= None sub = [] #visit each edge and each node of each edge for edge in graph: node1, node2 = edge if start_node in edge: if node1 == start_node: next_node = node2 else: next_node = node1 if not visited(next_node, path): # neighbor node not on path yet sub = [next_node] sub.extend(path) # explore extended path findNewCycles(sub); e...

Cách đọc test đầu vào Python

Link Đề: https://codeforces.com/problemset/problem/1369/F Code: import itertools as it import collections as cc import heapq as hp import sys I = lambda : list ( map ( int , input (). split ())) import operator as op from functools import reduce n , m = I () w = I () de =[ 0 ]* n for i in range ( n ): de [ i ]=[ 0 ] pre =[ 0 ]* m visi =[ 0 ]* m for i in range ( m ): x , y = I () x -= 1 ; y -= 1 pre [ i ]=[ x , y ] de [ x ][ 0 ]+= 1 de [ y ][ 0 ]+= 1 de [ x ]. append ( i ) de [ y ]. append ( i )   an =[] te =[] ans =[] for i in range ( n ): if de [ i ][ 0 ]<= w [ i ]: te . append ( i )   while len ( te )> 0 : tem = te . pop () for i in range ( 1 , len ( de [ tem ])): fr = de [ tem ][ i ] if not visi [ fr ]: visi [ fr ]= 1 ans . append ( fr + 1 ) fo = pre [ fr ][ 0 ]+ pre [ fr ][ 1 ]- tem de [ fo ][ 0 ]-= 1 if de [ fo ][ 0 ]== w [ fo ]: te . append ( fo )   ...

Sinh Test trong Python va code AC

P1: #file = "C:/New folder/aka/1.inp" #with open(file) as f: # a,b=[int(x) for x in next(f).split()] #print(a) #print(b) def gg(id): file= "C:/New folder/aka/" +id+ ".inp" with open (file) as f: a,b=[ int (x) for x in next (f).split()] products = set () for pair in [(x,y) for x in range ( 2 ,a+ 1 ) for y in range ( 2 ,b+ 1 )]: products.add(pair[ 0 ]**pair[ 1 ]) return len (products) x= range ( 1 , 101 ) for i in x: id= str (i) file1 = "C:/New folder/aka/" +id+ ".out" f= open (file1, "w" ) f.write( str (gg(id))) f.close() P2: a,b=[ int (x) for x in input ().split()] product = set () for pair in [(x,y) for x in range ( 2 ,a+ 1 ) for y in range ( 2 ,b+ 1 )]: product.add(pair[ 0 ]**pair[ 1 ]) print ( len (product))

Check nguyen to thong minh

ll check(ll n){ if(n<=1) return 0; if(n<=3) return 1; if(n%2==0||n%3==0) return 0; for(ll i=5;i*i<=n;i+=6) if(n%i==0||n%(i+2)==0) return 0; return 1; }

Viết checker (tong ami)

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) {     ifstream inp(argv[1]);     ifstream out(argv[2]);     ifstream ans(argv[3]);     int n, a, b, c, d;       inp >> n;     out >> a >> b;     ans >> c >> d;     if (a + b == c + d) {         cout << a << " + " << b << " = " << c << " + " << d << endl;         return 0; // AC     }        else {         cout << "a + b = " << a + b << " != " << n << endl;         return 1; // WA     } } /***********************/ #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn (ll)(105) ll pos1,length,pos2,i,n,dem,size_b; ll a[maxn],b[maxn],c[maxn];...

Sinh test (input và output đồng thời)

#include<bits/stdc++.h> using namespace std; long long Rand(long long l, long long h) { return l + ((long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) * (RAND_MAX + 1) +             (long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) +             (long long)rand() * (RAND_MAX + 1) +             rand()) % (h - l + 1); } void write(string t,string t1){    ofstream fileOutput(t);    ofstream fileOutput1(t1);    if(fileOutput.fail()){      cout<<"Cannot open file at "<<t<<'\n';    }    long long tmp=Rand(1,1000);    fileOutput<<tmp<<'\n';    if(tmp<=2) fileOutput1<<tmp+2<<'\n';    else fileOutput1<<tmp-2<<'\n'; } int main(){     srand(time(NULL));     int i;     string s="",s...

Sinh Test

#include < bits / stdc ++. h > using namespace std ; long long Rand ( long long l , long long h ) { return l + (( long long ) rand () * ( RAND_MAX + 1 ) * ( RAND_MAX + 1 ) * ( RAND_MAX + 1 ) + ( long long ) rand () * ( RAND_MAX + 1 ) * ( RAND_MAX + 1 ) + ( long long ) rand () * ( RAND_MAX + 1 ) + rand ()) % ( h - l + 1 ); } void write ( string t ){ ofstream fileOutput ( t ); if ( fileOutput . fail ()){ cout << "Cannot open file at " << t << '\n' ; } fileOutput << Rand ( 1 , 1000 )<< '\n' ; } int main (){ srand ( time ( NULL )); int i ; string s = "" ; for ( i = 0 ; i < 100 ; i ++){ s = "C:/New folder/aka/" + to_string ( i + 1 )+ ".inp" ; write ( s ); } return 0 ; }