#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #define LL long long using namespace std; LL n,m,A[][],p[],pos,d[],r[],len,B[][]; ]={}; void prime() { pos=; ;i<;i++) { if(!vd[i])…
<?php //用数组 function fib($n){ $array = array(); $array[0] = 1; $array[1] = 1; for($i=2;$i<$n;$i++){ $array[$i] = $array[$i-1]+$array[$i-2] . ','; } print_r($array); } fib(20); // echo "\n------------------\n"; function fib_recursive($n){ i…
<?php function Fibonacci($n){ if ($n <= 0) { return 0; } elseif ($n == 1) { return 1; } else { return Fibonacci($n - 1) + Fibonacci($n - 2); } } for($i=1;$i<=20;$i++){ echo Fibonacci($i); echo "  "; } ?>…
HIGH - Highways no tags  In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a lis…
C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue&…
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po…
利用动态规则的思路,摒弃传统的递归做法,可以得到一种快速的求fibonacci第n个数的算法: ''' 求第n(从1开始)位fibonacci数 fibonacci数列前两位为0, 1. 后面每一位数字等于前两位数字之和 ''' def fibonacci( n ): if n <= 2: return n - 1 f = 0 g = 1 while n - 2 > 0: g = g + f f = g - f n -= 1 return g print( fibonacci( 100 ) )…
2013-09-07 10:50:31 面试题36:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字构成一个逆序对.输入一个数组,求出这个数组中逆序对的总数. 小结: 最直观的的方法是:对每个数字,测试后面的数字是否小于该数字,这种方法的时间复杂度为O(N^2): 为了改善时间性能,用归并的方法,但这种方法组要辅助的空间O(N),见下面函数GetNumberOfInversePairs. 代码(测试暂未发现错误,欢迎交流指正!): #include <iostream> #inc…
/* Date: 07/03/19 15:40 Description: 用递归法求n阶勒让德多项式的值      { 1  n=0    Pn(x)= { x  n=1      { ((2n-1).x-Pn-1(x)-(n-1).Pn-2(x)/n  n>=1 */ #include<stdio.h> float Legendre(int x,int n); int main(void) { int x,n; float value; printf("Enter the o…
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需要给定\(01/10,00,11\)的关系,总共\(8\)种. 如果是暴力的话,我们的方法有两种, 第一种是比较喜闻乐见的, 我们对于当前数\(x\),暴力计算所有存在的数\(a_i\)中,\(x\oplus a_i\)的最大值,这样的复杂度是\(O(2^{16})\)的. 另外一种也是不难考虑到的…
解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中位数的方法很巧妙,非常值得借鉴,这里写一个用类似思想实现 求第k个最小数的值 这里没有虚加 #,因为求k个最小数的值 不需要考虑 奇偶问题,所以更简单,代码如下: //[2,3,5] [1 4 7 9] 第k个小的树的数,比如k=3 那么就返回3 int findTopKSortedArrays(vector…
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数的除法~   除法运算:被除数中包含有多少个除数的计算   由于是int类型的除法,因此结果可能超过int的最大值,当超过int的最大值时输出int的最大值   另写除法函数,计算出除法的商. 首先判断出除法运算后的结果是正数还是负数. 之后需要将被除数和除数都变为正数,进行进一步计算 当被除数小于…
题目:(来自光荣之路老师)a+b==valuea+b+c=valuea+b+c+d==valuea+b+c+d+...=valuea和b....取值范围都在0-value写一个方法 传进去列表和预期得value  求出所有变量得取值可能性 一个有顺序得数字序列  从小到大 不限制个数 序列里面随机两个数相加为value得可能性例子[1,2,3,4,5,6,12,19] value为2019+1==20只有一种可能性要求时间复杂度为O(n) 代码: #encoding=utf-8 seq=[1,1…
====数组篇==== 2.1 求最小的k个数: 题目描述:有n个整数,请找出其中最小的k个数,要求时间复杂度尽可能低. 解法一: 思路:快排后输出前k个元素,O(nlogn). writer: zzq function: 给定一个数组,寻找数组中最小的k个数. 方法一: 先对数组进行排序(快排), 然后选择前k个数. 快排思想: 分治+挖坑 挖坑: 1) 先找到一个基准值a[i],存到key里面,然后把a[i]挖空: 2) 从j开始往前找(j--),找到第一个比key小的数,就用当前的a[j]…
题目链接:http://codeforces.com/problemset/problem/546/D 题意: 给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n,最后当n变成1的时候经过了几轮得分就是这个轮数,要求最大的分数是多少 思路: 很明显,就是一个求整数质因子个数的题目,阶乘我们不需要算,我们知道在a>b的时候,b!都约掉了,那么我们只需压迫计算出每个数的质因数有几个,然后计算出1~n的质因子之和,那么就可以迅速得到答案了 #include<iostream> #i…
方法一:朴素算法:O(n). #include<bits/stdc++.h> using namespace std; int get_num(int n){ ; ;i<=n;++i) )num++; return num; } int get_sum(int n){ ; ;i<=n;++i) )tot+=i; return tot; } int main(){ int n; while(cin>>n){ cout<<get_num(n)<<en…
题目描述 输入描述: 输入数据共一行,一个正整数n,意义如“问题描述”. 输出描述: 输出一行描述答案:一个正整数k,表示S的末尾有k个0 输入例子: 10 输出例子: 7 --> 示例1 输入 10 输出 7 说明 解题思路:求1~n每个数的阶乘相乘后尾数为0的个数. AC代码一(365ms):暴力枚举1~n也能过?说明测试数据不大.采用法一可以过:直接统计每个元素包含因子为5的个数.但采用法二会TLE,原因同样是暴力枚举,但每次需要重新计算当前i的因子为5的个数,而法一让很多数可以省去这一步…
php实现求最小的k个数(日常出错很容易是分号或者$符号忘记写了) 一.总结 日常出错很容易是分号或者$符号忘记写了 二.php实现求最小的k个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 三.代码 <?php function GetLeastNumbers_Solution($input, $k) { if(count($input)<$k) return []; //1.$符号忘记写 2.返回空数…
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还有一种据说速度更快的,没测试过两者的速度差别 l1 = ['b','c','d','b','c','a','a'] l2 = {}.fromkeys(l1).keys() print l2 这两种都有个缺点,祛除重复元素后排序变了: ['a', 'c', 'b', 'd'] 如果想要保持他们原来的排…
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left…
HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K [Problem Description] 略 [Solution] 对于一个指数\(k\),找到一个最大的\(m\)使得\(m^k\le n\),则\(k\)这个指数对答案的贡献为\(m\),因为对于\(i\in[1,m]\)中的数\(i^k\)一定小于等于\(n\).而\(m=n^{\frac{1}{k}}\).由唯一分解定理可知,\(k\)一定能表示为一些素数的乘积.所以只需要考虑\(64\)以内的素数即可.但是会出现重…
问题:A,B,C,D分别为不同的整数,满足以下乘法公式,求A,B,C,D的值 解题思路: 由题意可知A,B,C,D为不同的整数,则A!=B,A!=C,A!=D,B!=C,B!=D,C!=D 再由给出公式的解题步骤可知A,B,C,D其中一位数是0,则C=0,因公式中给出的9可得出A=3,也因此限制了这个四位数的千位是3: 再看解题步骤可知第一行代表:D*ABCD,第二行代表:B*ABCD,第三行代表:A*ABCD,因都是四位数,以此为限制条件. 最后看结果数为八位数,说明9往前进了一位也表明第二行…
筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1,t=i*p[1]; while(j<=p[0] && t<=r) { no[t]=true; if(i%p[j]==0) //每一个数字都有最小质因子.这里往后的数都会被筛过的,break break; t=i*p[++j]; } } } O(n)筛欧拉函数 void find()…
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> #define NUM 3 int Fun(int n, int a[NUM][NUM]); /*函数声明*/ int main() { , j = ; /*i,j分别表示行与列*/ ][] = { {,,},{,,},{,,} }; /*定义行列式*/ printf("%d\n&q…
// |A| * A- = A* (伴随矩阵) = 逆矩阵 * 矩阵的值 #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> using namespace std; ; ; int a[MAXN][MAXN], b[MAXN][…
http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose le…
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,an There are m queries. In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,⋯,ari. We can denote the…
X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2522    Accepted Submission(s): 790 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod…
Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11914   Accepted: 5519 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N…
题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例如: 15 = 7+8 = 4+5+6 = 1+2+3+4+5,所以15对应的答案是3,有三种; 我们现在相当于已知等差数列的和sum = n, 另首项为a1,共有m项,那么am = a1+m-1: sum = m*(a1+a1+m-1)/2  -----> a1 = sum/m - (m-1)/2 a…