Notes for writing code ., Write code

Source: Internet
Author: User

Notes for writing code ., Write code

I. Quick power

 

1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ */2/* force convert the type of values that may burst into an array */3/* And modulo the value mod */4/* const int mod (1e9 is double type) */5/* forced conversion to int */6 /*~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ */

 


1 int fast_pow (int n, int k) {2Int ans = 1;// Declare it within the function. 3 if (k = 0) 4 return 1; 5 for (; k> = 1, n = (LL) n * n % mod) 6 if (k & 1) 7 ans = (LL) ans * n % mod; 8 return ans; 9}

II. When querying the set compression path

Int find (int x) {if (fa [x] = x) return x; else {int t = fa [x]; fa [x] = find (t ); // equal to find (t) first, and then return;} return fa [x];}

3. Modulo

For (int I = 2; I <= k + 1; I ++) for (int j = 1; j <= I; j ++) {f [I] [j] = (LL) (f [I-1] [j] + f [I-1] [J-1]) % mod; // The modulo operation level is high. Brackets must be added for addition ;}

4. When reading a string

Char s [100]; scanf ("% s", s); // write only the variable name, not the size; scanf ("% s", s + 1 ); // starts from subscript [1;

Before reading the next string, you need to read an empty string (space at the end of the line and press Enter );

5. The variables in the function are preferably local variables.

Sat. When opening arrays and defining new variables, you must compare them with the data range in the original question to prevent out-of-the-boundary arrays or large waste of array opening.

7. Check the header file and read the output file before submitting'

Freopen (". in", "r", stdin); freopen (". out", "w". stdout); // especially this line. Do not comment ''with a slash''

8. Note that STL is similar.

Lower_bound (); upper_bound (); // perform binary search on an ascending array. // The former returns the first location greater than or equal to the query value; // The second returns the first location greater than the query value;

9. Generally, the problem of having the minimum or maximum values of several items to be the smallest should be solved by means of binary;

The second answer is mid. Check whether the problem meets the conditions. If yes, perform the second step in [mid + 1, r] and the second step in [l, mid.

10. Remember the Euclidean Algorithm and the Extended Euclidean Algorithm

Int gcd (int a, int B) {if (! B) return a; else return gcd (B, a & B) ;}// Euclidean algorithm.
Void exgcd (int a, int B, int & d, int & x, int & y) {if (! B) {d = a; x = 1; y = 0;} else {exgcd (B, a % B, d, y, x ); y-= (a/B) * x) ;}// extends the Euclidean algorithm.

Sometimes, x and y are negative, and the solution set is (x + k * B ', y-k * ');

B '= B/gcd (a, B );

A' = a/gcd (a, B );

11. Basic Properties of Modulus

(A + B) % B = (a % n) + (B % n) % n;

(A-B) % B = (a % n)-(B % n) + n) % n;

A * B % n = (a % n) * (B & n) % n;

Division cannot be modulo. If modulo is to be modulo, reverse yuan is required first;

12th. When the mark is used up, it must be cleared.

13th. Big data computing requires high precision.

Fourteen. 01 difference between a backpack and a full backpack

void ZeroOnePack(int f[],int V,int v,int w){    for(int i = V,i>=v;--i)       f[i] = max(f[i],f[i - v] + w);} 
void CompletePack(int f[],int V,int v,int w){    for(int i = v;i <= V;i++)       f[i] = max(f[i],f[i - v] + w);}
15th. Int a; int B; double sum;
When sum = a + B is calculated, a and B must be forcibly converted to the double type.
int a;int b;double sum;sum = (double) a + b;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.