USACO Arithmetic Progressions

最笨的辦法竟然經過了:
/*
ID: nenusb1
LANG: C
TASK: ariprog 
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    freopen("ariprog.in","r",stdin);
    freopen("ariprog.out","w",stdout);
    int n,m;
    scanf("%d", &n);
    scanf("%d", &m);
    int i,j;
    
//    例子: n=3,m=2,即求{0,1,2,4,5,8} 集合中有多少個長度爲3的等差數列 
    
    int sqr[255];
    int s[125001];//m 的最大值是250,平方數是62500,因此平方和最大爲62500 + 62500 = 125000 
    memset(sqr,0,sizeof(sqr));
    memset(s,0,sizeof(s));  
    
    //產平生方數 
    for(i=1; i<=m; i++){
          sqr[i] = i*i;
    }
    int count = 0;
    //產平生方和集合 
    for(i=0; i<=m; i++){
          for(j=i; j<=m; j++){
                int temp = sqr[i] + sqr[j];
                s[temp] = 1;
                count++;
          }
    }
    int max = sqr[m]  * 2;    

    int noneFlag = 1;    
    int diff = 1;
    while(   ((n-1)*diff) <= max       ){//這個條件很重要  
            for(i=0; i<=max-(n-1)*diff; i++){
                  int f1 = 1;
                  for(j=i; j<=i+(n-1)*diff; j+=diff){
                        if(s[j] == 0){ f1=0; break;}
                  }
                  if(f1) {
//                         printf("from i=%d with diff=%d\n",i,diff);
                           printf("%d %d\n",i,diff);
                           noneFlag = 0;
                  }
            }  
            diff++;        
    } 
    
    if(noneFlag){
         printf("NONE\n");
    }
           
    
    
   
    return 0;
}
相關文章
相關標籤/搜索