摘要:
原题链接设金片数量为n时所需要的步骤为F(n),则易得递推式F(n) = 2F(n-1) + 1; 易推得F(n) = 2^n - 1;但是一般的累乘容易超时,这题需要一个时间消耗为O(log n)的算法。附ac代码:#include #define mod 1000000long long power(int n){ //若n为偶数,则n个2相乘,等于前n/2个2相乘的平方 //若n为奇数,则n个2相乘,等于前n/2个2相乘的平方再乘以2 if(n == 1) return 2; long long t = power(n / 2) % mod; t = t * t % mod; if(n 阅读全文
摘要:
原题链接由于10可以分解为质因数2 * 5,所以可以将原阶乘分解求出2和5因子的数量,则末尾0的个数就是2和5中数量较少的那个的数量。附ac代码:#include int main(){ int t, n; scanf("%d", &t); while(t-- && scanf("%d", &n)){ int a = 0, b = 0; //分别存储质因子‘2’和、‘5’的数量 int x = n; //副本 while(x) a += (x /= 2); while(n) b += (n /= 5); printf(&q 阅读全文
摘要:
原题链接直接应用递推公式打表。附ac代码:#include int main(){ int t, n, a[41] = {0, 0, 1, 2}; for(int i = 4; i != 41; ++i) a[i] = a[i - 1] + a[i - 2]; scanf("%d", &t); while(t-- && scanf("%d", &n)) printf("%d\n", a[n]); return 0;} 阅读全文
摘要:
原题链接这题不需要考虑+123 +456的情况,所以就简单了一些。附ac代码:#include #include #define MAX 1000 + 1char a[MAX], b[MAX];int main(){ int x, y; while(scanf("%s%s", a, b) == 2){ if(a[0] == b[0] && a[0] == '0') break; x = strlen(a); y = strlen(b); if(a[0] != '-' && b[0] != '-' 阅读全文
摘要:
原题链接简单题,只要将乘客体重排序就好办了。附ac代码:#include #include int a[301];int cmp(const void *a, const void *b){ return *(int *)a - *(int *)b;}int main(){ int t, w, n, x, count; scanf("%d", &t); while(t-- && scanf("%d%d", &w, &n)){ x = n; while(x--) scanf("%d", & 阅读全文