├── readInt().cpp ├── scan().c ├── FastScanner.java ├── python-tactics-2 ├── Fast.java └── python-tactics-1 /readInt().cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int readInt () { 16 | bool minus = false; 17 | int result = 0; 18 | char ch; 19 | ch = getchar(); 20 | while (true) { 21 | if (ch == '-') break; 22 | if (ch >= '0' && ch <= '9') break; 23 | ch = getchar(); 24 | } 25 | if (ch == '-') minus = true; else result = ch-'0'; 26 | while (true) { 27 | ch = getchar(); 28 | if (ch < '0' || ch > '9') break; 29 | result = result*10 + (ch - '0'); 30 | } 31 | if (minus) 32 | return -result; 33 | else 34 | return result; 35 | } 36 | 37 | int main() 38 | { 39 | 40 | int n,k; 41 | n=readInt(); 42 | k=readInt(); 43 | int cnt=0; 44 | while(n--) 45 | { 46 | int num; 47 | num=readInt(); 48 | if(num%k==0)cnt++; 49 | } 50 | printf("%d",cnt); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /scan().c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //inline 4 | unsigned long long scan() 5 | { 6 | unsigned long long z=0; 7 | char c; 8 | do{ 9 | c=getchar_unlocked(); 10 | } while(c<'0'); 11 | 12 | for(;c>='0';c=getchar_unlocked()) 13 | z = (z<<3) + (z<<1) + (c&15); 14 | 15 | return z; 16 | } 17 | 18 | //inline 19 | void put_uint(unsigned long long n) 20 | { 21 | unsigned short stack[32]; 22 | int top = 0; 23 | do{ 24 | stack[top++] = n % 10 + '0'; 25 | n /= 10; 26 | } while(n != 0); 27 | 28 | while(top > 0) 29 | putchar_unlocked(stack[--top]); 30 | 31 | putchar_unlocked('\n'); 32 | 33 | } 34 | 35 | //inline 36 | int dgdum(unsigned long long n) 37 | { 38 | int sum = 0; 39 | while(n) 40 | { 41 | sum+=n%10; 42 | n/=10; 43 | } 44 | return sum; 45 | } 46 | 47 | int main() { 48 | 49 | int tc; 50 | unsigned long long n; 51 | scanf("%d",&tc); 52 | 53 | while(tc--) 54 | { 55 | int ok = 0; 56 | n=scan(); 57 | //int dg = dgdum(n); 58 | put_uint(n); 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /FastScanner.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.FileNotFoundException; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | public class Main { 10 | 11 | public static void main(String[] args){ 12 | FastScanner sc = new FastScanner(); 13 | int a = sc.nextInt(); 14 | long b = sc.nextLong(); 15 | double c = sc.nextDouble(); 16 | 17 | System.out.println(a); 18 | System.out.println(b); 19 | System.out.println(c); 20 | } 21 | 22 | public static class FastScanner { 23 | BufferedReader br; 24 | StringTokenizer st; 25 | 26 | public FastScanner(String s) { 27 | try { 28 | br = new BufferedReader(new FileReader(s)); 29 | } catch (FileNotFoundException e) { 30 | // TODO Auto-generated catch block 31 | e.printStackTrace(); 32 | } 33 | } 34 | 35 | public FastScanner() { 36 | br = new BufferedReader(new InputStreamReader(System.in)); 37 | } 38 | 39 | String nextToken() { 40 | while (st == null || !st.hasMoreElements()) { 41 | try { 42 | st = new StringTokenizer(br.readLine()); 43 | } catch (IOException e) { 44 | // TODO Auto-generated catch block 45 | e.printStackTrace(); 46 | } 47 | } 48 | return st.nextToken(); 49 | } 50 | 51 | int nextInt() { 52 | return Integer.parseInt(nextToken()); 53 | } 54 | 55 | long nextLong() { 56 | return Long.parseLong(nextToken()); 57 | } 58 | 59 | double nextDouble() { 60 | return Double.parseDouble(nextToken()); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /python-tactics-2: -------------------------------------------------------------------------------- 1 | ############################################## RE-MAP FUNCTIONS AT RUNTIME ############################################## 2 | 3 | Say you have a function 4 | 5 | class Test: 6 | def check(self,a,b,c): 7 | if a == 0: 8 | self.str = b*100 9 | else: 10 | self.str = c*100 11 | 12 | a = Test() 13 | def example(): 14 | for i in xrange(0,100000): 15 | a.check(i,"b","c") 16 | 17 | import profile 18 | profile.run("example()") 19 | 20 | And suppose this function gets called from somewhere else many times. 21 | 22 | Well, your check will have an if statement slowing you down all the time except the first time, so you can do this: 23 | 24 | class Test2: 25 | def check(self,a,b,c): 26 | self.str = b*100 27 | self.check = self.check_post 28 | def check_post(self,a,b,c): 29 | self.str = c*100 30 | 31 | a = Test2() 32 | def example2(): 33 | for i in xrange(0,100000): 34 | a.check(i,"b","c") 35 | 36 | import profile 37 | profile.run("example2()") 38 | 39 | 40 | 41 | ################################################### PYTHON IS NOT C ################################################### 42 | 43 | % timeit.py -s 'x = 47' 'x * 2' 44 | loops, best of 3: 0.574 usec per loop 45 | % timeit.py -s 'x = 47' 'x << 1' 46 | loops, best of 3: 0.524 usec per loop 47 | % timeit.py -s 'x = 47' 'x + x' 48 | loops, best of 3: 0.382 usec per loop 49 | 50 | There is a significant advantage in Python to adding a number to itself instead of multiplying it by two or shifting it left by one bit. 51 | 52 | 53 | 54 | ################################################### DATA AGGREGATION ################################################## 55 | 56 | Avoid : 57 | 58 | x = 0 59 | def doit1(i): 60 | global x 61 | x = x + i 62 | 63 | list = range(100000) 64 | t = time.time() 65 | for i in list: 66 | doit1(i) 67 | 68 | Use : 69 | 70 | x = 0 71 | def doit2(list): 72 | global x 73 | for i in list: 74 | x = x + i 75 | 76 | list = range(100000) 77 | t = time.time() 78 | doit2(list) 79 | 80 | Even written in Python, the second example runs about four times faster than the first. Had doit been written in C the difference would likely have been even greater (exchanging a Python for loop for a C for loop as well as removing most of the function calls). 81 | -------------------------------------------------------------------------------- /Fast.java: -------------------------------------------------------------------------------- 1 | import java.io.DataInputStream; 2 | import java.io.FileInputStream; 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | public class NightChanges { 7 | 8 | private static Reader in; 9 | private static PrintWriter out; 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | in = new Reader(); 14 | out = new PrintWriter (System.out, true); 15 | //------------ 16 | 17 | 18 | 19 | out.close(); 20 | in.close(); 21 | } 22 | 23 | static class Reader { 24 | final private int BUFFER_SIZE = 1 << 16; 25 | private DataInputStream din; 26 | private byte[] buffer; 27 | private int bufferPointer, bytesRead; 28 | public Reader(){ 29 | din=new DataInputStream(System.in); 30 | buffer=new byte[BUFFER_SIZE]; 31 | bufferPointer=bytesRead=0; 32 | } 33 | 34 | public Reader(String file_name) throws IOException{ 35 | din=new DataInputStream(new FileInputStream(file_name)); 36 | buffer=new byte[BUFFER_SIZE]; 37 | bufferPointer=bytesRead=0; 38 | } 39 | 40 | public String readLine() throws IOException{ 41 | byte[] buf=new byte[64]; // line length 42 | int cnt=0,c; 43 | while((c=read())!=-1){ 44 | if(c=='\n')break; 45 | buf[cnt++]=(byte)c; 46 | } 47 | return new String(buf,0,cnt); 48 | } 49 | 50 | public int nextInt() throws IOException{ 51 | int ret=0;byte c=read(); 52 | while(c<=' ')c=read(); 53 | boolean neg=(c=='-'); 54 | if(neg)c=read(); 55 | do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 56 | if(neg)return -ret; 57 | return ret; 58 | } 59 | 60 | public long nextLong() throws IOException{ 61 | long ret=0;byte c=read(); 62 | while(c<=' ')c=read(); 63 | boolean neg=(c=='-'); 64 | if(neg)c=read(); 65 | do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 66 | if(neg)return -ret; 67 | return ret; 68 | } 69 | 70 | public double nextDouble() throws IOException{ 71 | double ret=0,div=1;byte c=read(); 72 | while(c<=' ')c=read(); 73 | boolean neg=(c=='-'); 74 | if(neg)c = read(); 75 | do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 76 | if(c=='.')while((c=read())>='0'&&c<='9') 77 | ret+=(c-'0')/(div*=10); 78 | if(neg)return -ret; 79 | return ret; 80 | } 81 | 82 | public char nextChar() throws IOException{ 83 | byte c=read(); 84 | while(c<=' ')c=read(); 85 | return (char)c; 86 | } 87 | 88 | private void fillBuffer() throws IOException{ 89 | bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE); 90 | if(bytesRead==-1)buffer[0]=-1; 91 | } 92 | 93 | private byte read() throws IOException{ 94 | if(bufferPointer==bytesRead)fillBuffer(); 95 | return buffer[bufferPointer++]; 96 | } 97 | 98 | public void close() throws IOException{ 99 | if(din==null) return; 100 | din.close(); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /python-tactics-1: -------------------------------------------------------------------------------- 1 | ############################################ STRING CONCATENATION ############################################ 2 | 3 | Avoid this: 4 | 5 | s = "" 6 | for substring in list: 7 | s += substring 8 | 9 | Use: 10 | 11 | slist = [some_function(elt) for elt in somelist] 12 | s = "".join(slist) 13 | 14 | Avoid: 15 | 16 | out = "" + head + prologue + query + tail + "" 17 | 18 | Instead, use 19 | 20 | out = "%s%s%s%s" % (head, prologue, query, tail) 21 | 22 | Even better, for readability (this has nothing to do with efficiency other than yours as a programmer), use dictionary substitution: 23 | 24 | out = "%(head)s%(prologue)s%(query)s%(tail)s" % locals() 25 | 26 | 27 | 28 | #################################################### LOOPS #################################################### 29 | 30 | Instead of looping over a list of words and converting them to upper case: 31 | 32 | newlist = [] 33 | for word in oldlist: 34 | newlist.append(word.upper()) 35 | 36 | you can use map to push the loop from the interpreter into compiled C code: 37 | 38 | newlist = map(str.upper, oldlist) 39 | 40 | List comprehensions were added to Python in version 2.0 as well. They provide a syntactically more compact and more efficient way of writing the above for loop: 41 | 42 | newlist = [s.upper() for s in oldlist] 43 | 44 | Generator expressions were added to Python in version 2.4. They function more-or-less like list comprehensions or map but avoid the overhead of generating the entire list at once. Instead, they return a generator object which can be iterated over bit-by-bit: 45 | 46 | iterator = (s.upper() for s in oldlist) 47 | 48 | Which method is appropriate will depend on what version of Python you're using and the characteristics of the data you are manipulating. 49 | 50 | 51 | 52 | ################################################ AVOIDING DOTS ################################################ 53 | 54 | Suppose you can't use map or a list comprehension? You may be stuck with the for loop. The for loop example has another inefficiency. Both newlist.append and word.upper are function references that are reevaluated each time through the loop. The original loop can be replaced with: 55 | 56 | upper = str.upper 57 | newlist = [] 58 | append = newlist.append 59 | for word in oldlist: 60 | append(upper(word)) 61 | 62 | This technique should be used with caution. It gets more difficult to maintain if the loop is large. Unless you are intimately familiar with that piece of code you will find yourself scanning up to check the definitions of append and upper. 63 | 64 | 65 | 66 | ############################################### LOCAL VARIABLES ############################################### 67 | 68 | Python accesses local variables much more efficiently than global variables. 69 | 70 | def func(): 71 | upper = str.upper 72 | newlist = [] 73 | append = newlist.append 74 | for word in oldlist: 75 | append(upper(word)) 76 | return newlist 77 | 78 | 79 | 80 | ####################################### INITIALIZING DICTIONARY ELEMENTS ###################################### 81 | 82 | Avoid: 83 | 84 | wdict = {} 85 | for word in words: 86 | if word not in wdict: 87 | wdict[word] = 0 88 | wdict[word] += 1 89 | 90 | Use: 91 | 92 | wdict = {} 93 | for word in words: 94 | try: 95 | wdict[word] += 1 96 | except KeyError: 97 | wdict[word] = 1 98 | 99 | It's important to catch the expected KeyError exception, and not have a default except clause to avoid trying to recover from an exception you really can't handle by the statement(s) in the try clause. 100 | 101 | A third alternative became available with the release of Python 2.x. Dictionaries now have a get() method which will return a default value if the desired key isn't found in the dictionary. This simplifies the loop: 102 | 103 | wdict = {} 104 | get = wdict.get 105 | for word in words: 106 | wdict[word] = get(word, 0) + 1 107 | 108 | Also, if the value stored in the dictionary is an object or a (mutable) list, you could also use the dict.setdefault method, e.g. 109 | 110 | wdict.setdefault(key, []).append(new_element) 111 | 112 | You might think that this avoids having to look up the key twice. It actually doesn't (even in python 3.0), but at least the double lookup is performed in C. 113 | 114 | Another option is to use the defaultdict class: 115 | 116 | from collections import defaultdict 117 | 118 | wdict = defaultdict(int) 119 | 120 | for word in words: 121 | wdict[word] += 1 122 | --------------------------------------------------------------------------------