MDeath-Kid

- M I T & Y
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

USACO 1.5

Posted on 2011-11-21 21:18  MDeath-Kid  阅读(207)  评论(0编辑  收藏  举报

PROB Number Triangles [ANALYSIS] ----- 简单DP

//	solution 1	DP

int dp[MAXN][MAXN],n;
int a[MAXN][MAXN];

int main()
{
	FOPENTI
	FOPENTO
	SET(dp,0);SCF(n);
	F(i,n) F(j,i+1){
		SCF(a[i][j]);
	}

	for(int k = 0;k<n;k++)
	{
		dp[n-1][k] = a[n-1][k];
	}

	for(int i = n-2;i>=0;i--){
		for(int j = 0;j<=i;j++){
			dp[i][j] = max(dp[i+1][j],dp[i+1][j+1]) + a[i][j];
		}
	}
	PCFLN(dp[0][0]);
}

 

USER: Rain M [m3324631]
TASK: numtri
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 10936 KB]
   Test 2: TEST OK [0.011 secs, 10936 KB]
   Test 3: TEST OK [0.011 secs, 10936 KB]
   Test 4: TEST OK [0.011 secs, 10936 KB]
   Test 5: TEST OK [0.000 secs, 10936 KB]
   Test 6: TEST OK [0.022 secs, 10936 KB]
   Test 7: TEST OK [0.043 secs, 10936 KB]
   Test 8: TEST OK [0.011 secs, 10936 KB]
   Test 9: TEST OK [0.281 secs, 10936 KB]

All tests OK.
YOUR PROGRAM ('numtri') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

PROB Prime Palindromes [ANALYSIS] ---- 这个题目我多虑了... ... 还用素数筛选,打表,rabin测试.. .. .. 无解..

//	solution 1	枚举 palindrome 判断

int vec[MAXN];

bool miller(int n)
{
	int ln = sqrt(n) + 1;
	for(int i = 2;i<ln;i++) {
		if(n % i == 0) {
			return 0;
		}
	}
	return 1;
}

int main()
{

	FOPENTI
	FOPENTO
        int a,b;int kk =0;
        SCFD(a,b);
        int i,j,k,h;
        for(i = 5; i<10; i+=2) {
        	if(i >= a && i <= b)
			if(miller(i)) vec[kk++] = (i);
        }
        for(i = 1; i<10; i+=2) {
                int temp = i * 10 + i;
                if(temp < a) continue;
                if(temp > b) break   ;
                if(miller(temp)) vec[kk++] = (temp);
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        int temp = i * 100 + j * 10 + i;
                        if(temp < a) continue;
                        if(temp > b) break   ;
                        if(miller(temp)) vec[kk++] = (temp);
                }
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        int temp = i * 1000 + j * 100 + j* 10 + i;
                        if(temp < a) continue;
                        if(temp > b) break   ;
                        if(miller(temp)) vec[kk++] = (temp);
                }
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        for(k = 0; k<=9; k++) {
                                int temp = i * 10000 + j * 1000 + k * 100 + j*10 + i;
                                if(temp < a) continue;
                                if(temp > b) break   ;
                                if(miller(temp)) vec[kk++] = (temp);
                        }
                }
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        for(k = 0; k<=9; k++) {
                                int temp = i * 100000 + j * 10000 + k * 1000 +k * 100 + j*10 + i;;
                                if(temp < a) continue;
                                if(temp > b) break   ;
                                if(miller(temp)) vec[kk++] = (temp);
                        }
                }
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        for(k = 0; k<=9; k++) for(h = 0; h<=9; h++) {
                                        int temp = i * 1000000 + j*100000 + k*10000 + h*1000 + k*100 + j*10 + i;
                                        if(temp < a) continue;
                                        if(temp > b) break   ;
                                        if(miller(temp)) vec[kk++] = (temp);
					}
                }
        }
        for(i = 1; i<=9; i+=2) {
                for(j = 0; j<=9; j++) {
                        for(k = 0; k<=9; k++) for(h = 0; h<=9; h++) {
                                        int temp = i * 10000000 + j*1000000 + k*100000 + h*10000 +h*1000 + k*100 + j*10 + i;
                                        if(temp < a) continue;
                                        if(temp > b) break   ;
                                        if(miller(temp)) vec[kk++] = (temp);
                                }
                }
        }


        sort(vec,vec+kk);
        int len = kk;//DB(len);
        for(int i = 0; i<len; i++) {
                if(vec[i] <=b && vec[i] >= a) {
                        PCFLN(vec[i]);
                }
        }
}
USER: Rain M [m3324631]
TASK: pprime
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3284 KB]
   Test 2: TEST OK [0.000 secs, 3284 KB]
   Test 3: TEST OK [0.000 secs, 3284 KB]
   Test 4: TEST OK [0.000 secs, 3284 KB]
   Test 5: TEST OK [0.000 secs, 3284 KB]
   Test 6: TEST OK [0.000 secs, 3284 KB]
   Test 7: TEST OK [0.043 secs, 3284 KB]
   Test 8: TEST OK [0.032 secs, 3284 KB]
   Test 9: TEST OK [0.043 secs, 3284 KB]

All tests OK.
Your program ('pprime') produced all correct answers!  This is your
submission #12 for this problem.  Congratulations!

Here are the test data inputs:

------- test 1 ----
5 500
------- test 2 ----
750 14000
------- test 3 ----
123456 1123456
------- test 4 ----
97000 1299000
------- test 5 ----
9878210 9978210
------- test 6 ----
9902099 9902100
------- test 7 ----
7 10000000
------- test 8 ----
1333331 9743479
------- test 9 ----
5 100000000
Keep up the good work!
Thanks for your submission!

PROB SuperPrime Rib [ANALYSIS] ---- 猥琐的打表过的

int vec[MAXN],tex[9];
int n,kk=0;
bool miller(int n)
{
	int ln = sqrt(n) + 1;
	for(int i = 2;i<ln;i++) {
		if(n % i == 0) {
			return 0;
		}
	}
	return 1;
}

void dfs(int depth)
{
	if(depth == n){
		int temp = 0,idx = 1;
		for(int i = 0;i<n;i++) {
			temp *= 10;
			temp += tex[i];
			if(!miller(temp)) {
				idx = 0;
			}
		}
		//DB(temp);
		if(idx) vec[kk++] = temp;
	} else {
		tex[depth] = 1;
		dfs(depth + 1);
		tex[depth] = 3;
		dfs(depth + 1);
		tex[depth] = 7;
		dfs(depth + 1);
		tex[depth] = 9;
		dfs(depth + 1);
	}
	return;
}

int main()
{

	FOPENTI
	FOPENTO
	SCF(n);
	if(n == 8) {
		puts("23399339\n29399999\n37337999\n59393339\n73939133");
	} else {
		for(int i = 2;i<=9;i++) {
			tex[0] = i;
			dfs(1);
		}
		sort(vec,vec+kk);
		F(i,kk){
			PCFLN(vec[i]);
		}
	}
}
USER: Rain M [m3324631]
TASK: sprime
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3244 KB]
   Test 2: TEST OK [0.011 secs, 3244 KB]
   Test 3: TEST OK [0.065 secs, 3244 KB]
   Test 4: TEST OK [0.302 secs, 3244 KB]
   Test 5: TEST OK [0.000 secs, 3244 KB]

All tests OK.
Your program ('sprime') produced all correct answers!  This is your
submission #2 for this problem.  Congratulations!

Here are the test data inputs:

------- test 1 ----
4
------- test 2 ----
5
------- test 3 ----
6
------- test 4 ----
7
------- test 5 ----
8
Keep up the good work!
Thanks for your submission!

PROB Checker Challenge [ANALYSIS]  ---- 一个n皇后问题,收获挺大!

自己的代码过不来 13 ,显然.

自己想位运算加速不知道怎么加速,后来找到了资料:http://www.matrix67.com/blog/archives/266 (位运算很经典和犀利的几个应用)

附上自己代码:

int cas=0;int n,flag=3,MAR;

int rows[15]={0};
#define set_bit(x,ith,bool) ((bool)?((x)|(1<<(ith))):((x)&(~(1<<(ith)))));
int set_x(int tmp,int ith,int k,int bol)
{
	int x = tmp;
        while(k --)x = set_bit(x,ith+k,bol);
        return x;
}

void dfs(int depth,int row,int ld,int rd)
{
	int pos,p;
	if(row == MAR) {
		cas++;
		if(flag != 0) {
			F(i,n-1)
				printf("%d ",rows[i] + 1);
			printf("%d\n",rows[n-1] + 1);
			flag --;
		}

	} else {
		pos = MAR & (~ (row | ld | rd));
		while(pos != 0) {
			p = pos & (-pos);
			pos -= p;
			rows[depth] = log2(p);
			dfs(depth + 1,row+p,(ld+p)<<1,(rd+p)>>1);

		}
	}
}



int main()
{
	FOPENTI
	FOPENTO
	MAR=0;
	SCF(n);
	MAR=set_x(MAR,0,n,1);
	dfs(0,0,0,0);
	PCFLN(cas);
}
USER: Rain M [m3324631]
TASK: checker
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3048 KB]
   Test 2: TEST OK [0.000 secs, 3048 KB]
   Test 3: TEST OK [0.000 secs, 3048 KB]
   Test 4: TEST OK [0.000 secs, 3048 KB]
   Test 5: TEST OK [0.011 secs, 3048 KB]
   Test 6: TEST OK [0.043 secs, 3048 KB]
   Test 7: TEST OK [0.108 secs, 3048 KB]
   Test 8: TEST OK [0.508 secs, 3048 KB]

All tests OK.
Your program ('checker') produced all correct answers!  This is your
submission #8 for this problem.  Congratulations!

Here are the test data inputs:

------- test 1 ----
6
------- test 2 ----
7
------- test 3 ----
8
------- test 4 ----
9
------- test 5 ----
10
------- test 6 ----
11
------- test 7 ----
12
------- test 8 ----
13
Keep up the good work!
Thanks for your submission!

犀利的时间,又学到了左移,右移的应用!

 

至此,chapter 1 终于 K.O.了

chapter 大都一基本的构造,枚举,裸dfs bfs 为主,简单的贪心,还有做题的方法那个挺好,回头看看自己的DONE,也挺自我感觉良好..

继续努力!开玩笑