摘要:Ugly Number可以表示为 [i, j, k] , i,j,k 分别表示 2 3 5 因子的个数;估计一下 i<=12 j <= 12, k<=12内有超过2000个,30^12 < 2^60,在long long 以内;关键是要按大小次序来,DISCUSS中有个大牛提了个很好的方法,我是按照他的方法来写的,0MS。# include <stdio.h># define MIN(x, y) ((x)<(y) ? (x):(y)) long long t[1505];int main(){ int i, j, k, p; t[0] = 1; i =
阅读全文
摘要:题意:给出 m 个长度都为 n 的字符串,按照逆序数的大小排序输出;简单的排序题,逆序数的计算,qsort练习;求逆序数有好的方法(O(n)),这里直接算也是0MS。# include <stdio.h>typedef struct { char s[51]; int w, id;}DNA;int m, n;DNA a[101];int DNA_cmp(const void *x, const void *y){ int tmp = (*(DNA*)x).w - (*(DNA*)y).w; if (tmp < 0) return -1; if ((tmp == 0...
阅读全文
摘要:给出一个R×C的地图,以及每个点的海拔,问下降序列最长多大?简单的DP,记忆化搜索;dp(i, j)if f[i, j]已经计算过 return f[i, j];f[i, j] = 1;for i = 1:k (ni, nj) = (i, j) + d[k]; if (ni, nj)合法 && h[i, j] > h[ni, nj] then f[i, j] = dp[ni, nj] + 1;return f[i, j];/* 滑雪:简单dp */# include <stdio.h># include <string.h># defin
阅读全文
摘要:准备有三种动物A,B,C,假设有A->B,B->C,那么有C->A。关系递推式:如果用R(x,y)表示x和y之间的关系,0表示同类,1表示x->y,2表示x<-y,那么有 R(x,z) = R(x,y) + R(y,z),如下表格 所以,对不不再一个集合中的两个元素x,y,R(x,y) = R(x,rx) + R(rx,ry) + R(ry,y)。练手POJ 1182 食物链1 TLE 原因是输入的时候是先输入d,我写成了scanf("%d%d%d", &x, &y, &d);优化:参考了杭杰的ppt,在每次查找时顺便更
阅读全文
摘要:Ozy的方法(dfs): 1 # include <stdio.h> 2 # include <string.h> 3 4 int f[51]; 5 int ok; 6 int length; 7 8 void dfs(int cnt, int len, int cur) 9 {10 int j;11 12 if (cnt == 0) {ok = 1; return;}13 14 --f[cur];15 16 len -= cur;17 if (len != 0)18 {19 j = len<cur ?...
阅读全文
摘要:bfs,RE多次之后学习大牛的代码,不要惊讶怎么限定在100000范围内!更好的解法:http://www.cnblogs.com/longzhiri/articles/1555344.html 1 # include <stdio.h> 2 # include <string.h> 3 # include <queue> 4 5 using namespace std; 6 7 int n, k, x; 8 int d[100002]; 9 queue<int>Q;10 11 int main()12 { 13 while (~scanf(&q
阅读全文
摘要:POJ的陷阱多多。。first WA:没考虑到一年中最后一天的情况,改了,也通过了一组discuss中的数据;2~3th WA:完全忽略了题目要求第一行输出test的个数;4th WA:输出格式中日的后面没有'.'。。。彻底服了 1 # include <stdio.h> 2 3 const char habbm[20][10] = {" ", "pop", "no", "zip", "zotz", "tzec", "xul",
阅读全文
摘要:直接枚举(16MS,可以接受),测试数据有点纠结。 1 # include <stdio.h> 2 3 int p, e, i, d; 4 const int add[] = {1, 23, 28, 23*28, 33, 23*33, 28*33}; 5 6 int mod(int x, int m); 7 8 int main() 9 {10 int cnt, day, sta;11 12 cnt = 0;13 while (1)14 {15 scanf("%d%d%d%d", &p, &e, &i, &d);16 ...
阅读全文
摘要:二分枚举;也可以将两个和数组都排序,这样可以在查找时保持沿一个方向,最坏情况下复杂度为O(n),不如二分查找;计算cpd[]时,c[i] + d[j]错写成c[i]+d[i]查了半天才发现。。。 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 # define MAXN 4001 5 6 int apb[MAXN*MAXN], cpd[MAXN*MAXN]; 7 int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; 8 9 int cmp(const void *a, const void *b
阅读全文
摘要:先计算幂乘表,根据输入n的二进制位判断是否乘该位相应的幂;以前做过类似的,但是当时递推矩阵不一样,这次拜读了Ozy的大作,自己敲了一遍;运行时间: 0ms,神奇吧! 1 /* poj 3070 */ 2 3 # include <stdio.h> 4 5 # define MAXN 30 6 # define MOD 10000 7 8 short int power[MAXN][4] = {{1,1,1,0}}; 9 short int ans[4];10 11 void mul(short int *a, short int *b, short int *c);12 13 in
阅读全文
摘要:求线性同余方程的最小非负解;gcd(a,n)=d;若d|b则有d个解,分布在[0,n-1]上,周期为n/d,所以取[0,n/d-1]上的即可(取模)。/* 谁让你试int型,WA活该 */ 1 # include <stdio.h> 2 3 long long int d, xx, yy; 4 5 long long int extended_euclid(long long int a, long long int b) 6 { 7 long long int t; 8 if (!b) d = a, xx = 1, yy = 0; 9 else 10 {1...
阅读全文
摘要:已经找到错误:10 100 330 这样10的倍数转换的不对# include <stdio.h># include <string.h>int main(){ int d, n, e, ans[150]; int i, j, tmp, c; char b[6];// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); while (scanf("%s%d", b, &e) != EO
阅读全文
摘要:直接枚举//2011-11-1#include <iostream>#include <cstdio>using namespace std;void Replace(char a[], int b[]);int Search(int a[]);bool Judge(int a[]);void Flip(int b[], int k);int main(){ char a[16], c; int b[16]; int i = 0; while ((c = getchar()) != EOF && i < 16) if (c == 'b
阅读全文
摘要:枚举:位运算版。谢谢大牛们指导!# include <iostream>using namespace std;const unsigned short int A[] = {0x111f, 0x222f, 0x444f, 0x888f, 0x11f1, 0x22f2, 0x44f4, 0x88f8, 0x1f11, 0x2f22, 0x4f44, 0x8f88, 0xf111, 0xf222, 0xf444,...
阅读全文