部分引用自:http://blog.csdn.net/v5zsq/article/details/77255048 所以假设方程 x^2+x+1=0 在模p意义下的解为d,则答案就是满足(ai/aj) mod p = d的数对(i,j)的数量(i<j). 现在把问题转化为解这个模意义下的二次方程. x^2+x+1=0 配方:x^2+x+1/4+3/4=0 (x+1/2)^2+3/4=0 同乘4:(2x+1)^2+3=0 即(2x+1)^2=-3 (mod p) 换句话说,我们必须保证-3+p是p…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/aj 在%p意义下. 解法:官方题解说是用二次剩余来解,但是我并不会这玩意了.在网上看到一位大佬没有二次剩余直接通过推公式做出了这题,真是神奇.http://www.cnblogs.com/bin-gege/p/7367337.html  将式子通分化简后可得(ai2+aj2+ai*aj)%p=0 .然后两…
Description 给nn个小于pp的非负整数a1,-,na1,-,n,问有多少对(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)模pp在意义下满足1ai+aj≡1ai+1aj1ai+aj≡1ai+1aj,即这两个数的和的逆元等于这两个数的逆元的和,注意0没有逆元 Input 第一行一整数TT表示用例组数,每组用例首先输入一整数nn表示序列长度和一素数pp表示模数,之后输入nn个非负整数a1,-,n(1≤T≤5,1≤n≤2×105,2≤p≤1018,0≤a1,-,n<p)a1…
题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants to know how many pairs i,j(1≤i<j≤n) are there, satisfying 1ai+aj≡1ai+1aj when we calculate module p, which means the inverse element of their sum equ…
Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the following integer sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, .…
http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:有一个a数列,并且每个数都小于p,现在要求有多少对$(i,j)$满足$\frac{1}{a_i+a_j} \equiv \frac{1}{a_i}+\frac{1}{a_j} \mod p$,0没有逆元. 思路:根据同余的性质对式子进行化简,在一个同余式两边同时做加法.减法或乘法仍保持同余. 先可以化简为 $1 \equiv 1+\frac{a_j}{a_i}+1+\frac{a_i}{a_j}…
在项目当中,经常出现需要根据Key值获取value:而且要求根据value获取key值,其实在commons-collections包中已经提供了此集合类.就是DualHashBidiMap类. (官方API:http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html) 其用法如下: /** * BidiMap,双向Map,可以通过key找到va…
You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to…
最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维护一个map,来记录0-i的和,我们希望 0~i + k = 0 ~ j ,这样就可以更新一次i~j的区间长度作为候补的结果. Time: O(n) Space: O(n) public class Solution { public int maxSubArrayLen(int[] nums, i…
Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent…