2014年9月27日 星期六

2014年9月8日 星期一

跑者的修練

#include <iostream>
using namespace std;
int main()
{
      int T;
      cin>>T;
      long long M,N;
      while(T--)
      {
           cin>>M>>N;
           long long ans=(1+N)*N/2;
           if(N>M)
                 ans+=(((N-M)+(N%M))*(int)(N/M))/2;
           cout<<ans<<endl;
      }
      return 0;
}


2014年8月23日 星期六

b024: 指南宮的階梯 (luke)

  1. /**********************************************************************************/  
  2. /*  Problem: b024 "指南宮的階梯" from 動態規劃-爬樓梯問題                                        */  
  3. /*  Language: C++                                                                 */  
  4. /*  Result: AC (4ms, 184KB) on ZeroJudge                                          */  
  5. /*  Author: luke at 2014-08-23 15:38:35                                           */  
  6. /**********************************************************************************/  
  7.   
  8. #include <iostream>  
  9. #include <iomanip>  
  10. #include <stdio.h>  
  11. using namespace std;  
  12. long long int a[100];  
  13. long long int solve( int number){  
  14.     if(a[number]!=0){  
  15.         return a[number];  
  16.     }  
  17.     if(number==1){  
  18.         a[1]=1;  
  19.         return 1;  
  20.     }  
  21.     if(number==2){  
  22.         a[2]=2;  
  23.         return 2;  
  24.     }  
  25.     else{a[number]=solve(number-1)+solve(number-2);  
  26.         return solve(number-1)+solve(number-2);}  
  27. }  
  28. int main () {  
  29.     int m;  
  30.     cin>>m;  
  31.     cout<<solve(m)<<" ";  
  32.     long long int k=solve(m);  
  33.     int v=k%m;  
  34.     cout<<solve(v);  
  35.       
  36.       
  37.        return 0;  
  38. }  

2014年8月21日 星期四

b021 "指考分發" (skipper)


  1. /**********************************************************************************/  
  2. /*  Problem: b021 "指考分發" from 排序-複合欄位                                             */  
  3. /*  Language: C++                                                                 */  
  4. /*  Result: AC (4ms, 200KB) on ZeroJudge                                          */  
  5. /*  Author: skipper at 2014-08-22 10:30:20                                        */  
  6. /**********************************************************************************/  
  7.   
  8. #include <iostream>  
  9. #include <algorithm>  
  10. #include <numeric>  
  11. using namespace std;  
  12. struct Score {  
  13.     int no,subj[5],total;  
  14. };  
  15. bool compare (Score a,Score b) {   
  16.     if (a.total>b.total) return true;      
  17.     if (a.total==b.total) return a.subj[2]>=b.subj[2];  
  18.     return false;   
  19. }  
  20.   
  21. int main ()  
  22. {  
  23.     int N; cin >> N;  
  24.     Score *s;  
  25.     s= new Score[N];  
  26.     for (int i=0;i<N;i++){  
  27.         cin >> s[i].no; s[i].total = 0;  
  28.         for (int j=0;j<5;j++) { cin >> s[i].subj[j];}  
  29.         s[i].total=accumulate(s[i].subj,s[i].subj+5,0);  
  30.     }  
  31.       
  32.     sort(s,s+N,compare);  
  33.     for (int i=0;i<N;i++){  
  34.         cout << s[i].no << endl;  
  35.     }  
  36. }  

執行時間測量(gcd)

#include <time.h>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int gcd1(int a, int b){
     for (int d = 1; d <= min(a, b); d++){
         if (a%d == 0 && b%d == 0) return d;
     }
}

int gcd2(int a, int b){
     if (a<b) swap(a, b);
     if (a%b)
         return b;
     else
         return gcd2(a - b, b);
}
// source: http://www.math.wustl.edu/~victor/mfmm/compaa/gcd.c
/* Standard C Function: Greatest Common Divisor */
int gcd(int a, int b)
{
     int c;
     while (a != 0) {
         c = a; a = b%ab = c;
     }
     return b;
}

/* Recursive Standard C Function: Greatest Common Divisor */
int gcdr(int a, int b)
{
     if (a == 0) return b;
     return gcdr(b%a, a);
}


void main(){
     clock_t begin = clock();
     // your CODE
     srand(clock());
     int x = rand();
     int y = rand();
     for (int i = 0; i < 100000000; i++){
         gcd1(x, y);
     }
     clock_t end = clock();  
     cout << end - begin;
     //system("pause");
}