
数学
hao_zong_yin
有问题可以加QQ讨论2987048728,备注一下优快云
展开
-
CodeForces - 385C Bear and Prime Numbers——素数筛法
在筛素数时计算每个数可以对应多少个题目中给定的数ai, 满足这个数是ai的因子, 然后预处理前缀和,O(1)完成查询#include #include #include #include using namespace std;const int maxn = 1e6;const int maxm = 1e7 + 5;int n, m, a[maxn], l, r;bool i原创 2017-11-24 16:30:00 · 300 阅读 · 0 评论 -
POJ - 1850 Code——组合数
题意:定义s为只由26个小写英文字母组成的长度任意的串,且串中的字母严格升序。定义集合S为长度10以下的s的集合,S中的元素优先按照串的长度排序,长度相同按照字典序排序。输入一个串str,若str在S中则输出str是S的第几小,若str不在S中输出0思路:组合数#include <cstdio>#include <cstring>#includ...原创 2018-05-08 17:50:52 · 243 阅读 · 0 评论 -
POJ - 1715 Hexadecimal Numbers——组合数
题意:给定一个k,求第k大的每位都不相同的16进制数思路:题目中要求第k大,可以转化一下求第(sum-k+1)小,这样好想一些。这就要求我们首先求出情况总数sum,这个可以用全排列公式来求,不过一定要注意前导0。之后就是普通的累加求和过程#include <cstdio>#include <cstring>#include <iostream...原创 2018-05-09 01:04:30 · 228 阅读 · 0 评论 -
POJ 1150 The Last Non-zero Digit——阶乘最后非0位
题解:https://blog.youkuaiyun.com/tsaid/article/details/7265759#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int table[5][5...原创 2018-05-09 21:12:18 · 181 阅读 · 0 评论 -
POJ - 2282 The Counting Problem——计数问题
果然纯数学的计数问题还是不适合我,放弃了#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;void Cal(char *s,int *num){ int i,j,k,n,t,m=atoi(s);...原创 2018-05-10 00:36:01 · 310 阅读 · 0 评论 -
HDU - 1085 Holding Bin-Laden Captive! ——母函数
题意:有三种硬币面值分别为1 2 5, 给定每种硬币的数量,问用这些硬币不能组成的最小面值是多少思路:因为每个硬币有个数限制,但是也不难构造出G(x)=(1+x+x^2+...+x^num1)(1+x^2+x^4+...+x^2*num2)(1+x^5+x^10+...+x^5*num5)将多项式展开后,xi项对应的系数就是组成面值为i的方案数。#include <...原创 2018-05-10 11:06:52 · 190 阅读 · 0 评论 -
HDU - 1521 排列组合——母函数
题意:有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。思路:G(x) = (1+x/1!+x^2/2!+...+x^a1/a1!) * (1 + x/1! + x^2/2! + ... + x^a2/a2!) *...*(1+x/1!+x^2/x!+...+x^an/an!)#include <cstdio>#include <cstring...原创 2018-05-10 15:57:45 · 482 阅读 · 0 评论 -
POJ - 1061 青蛙的约会——扩展欧几里得
一年前刷的题,然而一年没刷数学了。。。。。。题意:A开始在p点,每次可以前进m,B开始在q点,每次可以前进n,坐标轴是一个长度为L的环,问A前进多少次后可以遇到B思路:设一个可行的前进次数为x,那么可以列出方程:(p+x*m)%L = (q+x*n)%L转化一下:(x*(m-n))%L = (q - p)%L设(x*(m-n))与(q-p)之间相差y个L则:...原创 2018-05-10 18:05:34 · 210 阅读 · 0 评论 -
POJ - 2142 The Balance——扩展欧几里得
题意:给定a,b,c,求x,y,使得ax + by = c,题目保证有解,多解时另|x|+|y|最小,若还是多解则让|x|*a+|y|*b最小思路:首先交换一下a和b保证a>b(注意输出顺序也会受到影响),然后观察式子:|x|+|y| = |x0+b/gcd(a,b)*t| + |y0 - a/gcd(a, b)*t|因为我们规定a > b,所以式子的变化率主要由y...原创 2018-05-10 20:39:11 · 236 阅读 · 0 评论 -
POJ - 2891 Strange Way to Express Integers——扩展欧几里得解同余方程
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5 + 10;ll a[maxn], r[maxn];int...原创 2018-05-11 00:27:31 · 230 阅读 · 0 评论 -
HDU - 5514 Frogs——容斥
假设a和b都是m的因子,设t=lcm(a,b),若t<m,则t也是m的因子根据这个结论我们可以在m的因子中进行容斥,因子用fac数组保存,设vis[i]为fac[i]需要贡献的数量,num[i]表示fac[i]实际贡献的数量,那么fac[i]应该对答案贡献(等差数列之和)*(vis[i]-num[i]),然后更新后面的因子j(i+1<=j<=因子总数),num[j] += (...原创 2018-10-13 17:17:33 · 219 阅读 · 0 评论 -
HDU - 5985 Lucky Coins——概率
题意:有n种硬币,每种硬币有num个,扔一次正面向上的概率为p,现在同时对所有硬币进行操作,每次操作将所有硬币扔一次,舍弃背面朝下的,若干次操作后只剩下一种硬币,这种硬币就是幸运硬币,问每种硬币成为幸运硬币的概率思路:设a[i][j]为第i个硬币,扔j次后没有正面向上的硬币的概率,则a[i][j]=(1-p[i]^j)^num[i]设b[i][j]为第i个硬币,扔j次后至少有一个硬币正...原创 2018-10-03 20:05:23 · 223 阅读 · 0 评论 -
ACM-ICPC 2018 徐州赛区网络预赛 D Easy Math——杜教筛
这个式子可以递归计算,边界为:m=0:0m=1:u(n);n=1:segma{u(i)|1<=i<=m}其中segma{u(i)|1<=i<=m}可以用杜教筛在O(m^(2/3))内求出求解过程中要求的u(i)都可以O(1)求出,做法是先将n唯一分解,然后枚举子集,判一下元素个数,保存下来以后就可以O(1)求了注意判一下n是否有平方因子,有的话直...原创 2018-09-10 12:45:53 · 584 阅读 · 0 评论 -
矩阵快速幂模板
#define maxn 10+5;typedef long long ll;const ll mod = 1e9+7;#define clr(x,y) memset(x,y,sizeof(x))struct matrix { int n; ll maze[maxn][maxn]; void init(int n) { this->n=n; clr(maze,0);...原创 2018-02-13 21:53:15 · 150 阅读 · 0 评论 -
HDU - 1757 A Simple Math Problem——矩阵快速幂
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int k, m;struct Matrix { int mat[15][15]; Matrix() { memset(mat, 0, ...原创 2018-02-14 13:47:55 · 255 阅读 · 0 评论 -
HDU 1575 Tr A——矩阵快速幂
模板题#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 15;const int mod = 9973;int T, n, k;struct Matrix { ...原创 2018-02-14 14:13:20 · 244 阅读 · 0 评论 -
Gym - 101350G Snake Rana——容斥
容斥裸题#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e4 + 10;const int INF = 0x3f3f3f3f;int T, n, m, k;...原创 2018-10-08 11:34:32 · 255 阅读 · 0 评论 -
Gym - 101350E Competitive Seagulls——博弈
水题#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main() { int T, n; scanf("%d", &T); while (T--) { ...原创 2018-04-10 20:03:23 · 266 阅读 · 0 评论 -
FZU - 2282 Wand——错排公式+组合数
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e4 + 10;const int mod = 1e9 + 7;long long dp[maxn], c[max...原创 2018-04-13 10:15:12 · 212 阅读 · 0 评论 -
51Nod - 1244 莫比乌斯函数之和——杜教筛
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 5e6+10;bool vis[maxn];int mu[maxn], sum[maxn], prime_cnt, prime[maxn];map<ll, ll> MAP;void init() ...原创 2018-09-09 22:30:11 · 293 阅读 · 0 评论 -
UVALive 4119 Always an integer——数学归纳法+小模拟
首先开个结构体把每个C和E以及最后那个D膜出来然后算出E的最大值k,将数值1~k+1带入n验证,全部正确则Always an integer,否则Not always an integer下面证明一下为什么只验证1~k+1就可以,我们用数学归纳法进行验证:当k=0时,式子中没有变量,可以将n视为1来验证式子当k=1时,整个式子为n的一次函数,可设f(n) = an+b,则f(n+1...原创 2018-09-06 22:11:06 · 207 阅读 · 0 评论 -
UVA 11426 GCD - Extreme (II)——筛
虽然可以直接莫比乌斯,不过本着学学别的思路的原则,还是乱筛了一下(其实和莫比乌斯思路挺像)#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 4000001;ll phi[maxn], f[maxn], s[maxn];void init() { ph...原创 2018-09-06 22:43:11 · 191 阅读 · 0 评论 -
HDU 3988 Harry Potter and the Hide Story——唯一分解定理
题意:给定n和k,求一个最大的x,使得n! % k ^ x == 0思路:打出素数表,用每个素数分解n!和k,得到y和x,ans = min(ans,y/x)对m!阶乘唯一分解可以写作:while (m) { m /=prime[i]; y += m; }类似于统计m以内有多少个p...原创 2018-05-11 12:14:52 · 357 阅读 · 0 评论 -
HDU - 1576 A/B——逆元
裸题#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;ll exgcd(ll a, ll b, ll& x, ll& y) { i...原创 2018-05-11 14:20:30 · 191 阅读 · 0 评论 -
HDU - 3923 Invoker——Polya+费马小定理
裸的Polya,最后ans/(2*n)mod(1e9+7)时可以用费马小定理#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int mod ...原创 2018-05-18 23:10:40 · 195 阅读 · 0 评论 -
POJ - 2154 Color——Polya+欧拉函数优化
ans = 1/n*∑(0<=i && i < n)(n^gcd(i, n)) = 1/n*∑(0<=i && i < n && n%i == 0)(euler(i) * n^i) = ∑(0<=i && i < n && n%i ==原创 2018-05-19 21:12:46 · 202 阅读 · 0 评论 -
HDU 3547 DIY Cube——Polya
题意:用n种颜色染立方体的8个角,问有多少种不同的方法思路:共有24种置换,全部写出即可得到结果6x^2+17x^4+x^8import java.io.*;import java.math.*;import java.util.*;public class Main { public static void main(String [] args) { Scann...原创 2018-05-20 00:16:41 · 200 阅读 · 0 评论 -
POJ - 2888 Magic Bracelet——Polya+欧拉函数+dp+矩阵快速幂+费马小定理
题意:给定n个珠子构成的环,每个珠子有m种染色方式,给定k个二元组(xi,yi),表示颜色xi不能和颜色yi相邻,问有这个环多少种本质不同的染色方案(两种染色方案视为相同当且仅当他们可以通过旋转变成相同的方案),最终结果取余9973。(1<=n<=1e9 && gcd(n, 9973) = 1), (1 <= m <= 10), (1 <= k...原创 2018-05-20 17:22:49 · 225 阅读 · 0 评论 -
2017 乌鲁木齐区域赛 Sum of the Line——容斥
思路:先令 T 中所有位置的值为相应的c,那么答案为 1^2+2^2+...+k^2。下面考虑删去 T(r,c)=0 的位置造成的代价,也就是计算不和k互质的数对答案的贡献。略加思考就可以得到一个结论,删除第c个数(值肯定也为c)花费的代价一定是c^2暴力直接求的话会发现有的k的合数有上万个,铁定超时。考虑对k进行唯一分解,发现k分解后的素数最多也就九个,可以在2^9内用容斥原理进...原创 2018-08-13 01:20:20 · 573 阅读 · 0 评论 -
HDU 1695 GCD——莫比乌斯反演
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5 + 10;int cnt, prime[maxn], mu[ma...原创 2018-08-15 11:43:02 · 226 阅读 · 0 评论 -
HDU 6390 GuGuFishtion——莫比乌斯反演
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e6 + 5;int mod;bool vis[maxn];int...原创 2018-08-16 00:49:31 · 237 阅读 · 0 评论 -
Gym - 101550E Exponial——欧拉降幂
a^b%c = a^(phi(c)+b%phi(c))%c1e9看似吓人,其实中间模数为1时可以直接返回0,算是个强剪枝#include <bits/stdc++.h>using namespace std;typedef long long LL;LL pow(LL x, LL y, LL mod) { LL ans = 1; while (y) {...原创 2018-10-08 16:17:58 · 451 阅读 · 0 评论 -
HDU - 5955 Guessing the Dice Roll——ac自动机+概率dp+高斯消元
要写的就是代码中的solve函数,看一下就明白了,其余的都是模板,挺水的#include <bits/stdc++.h>using namespace std;const int maxn = 105;int T, N, L, data[maxn], id[maxn];double a[maxn][maxn], b[maxn];void Gauss(int num) {...原创 2018-10-09 12:11:57 · 245 阅读 · 0 评论 -
POJ - 1286 Necklace of Beads——Polya
裸的Polya注意n可能是0,不处理的话最后/0会RE#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;ll gcd(ll a, ll b) { re...原创 2018-05-18 22:50:02 · 162 阅读 · 0 评论 -
POJ2409 Let it Bead——Polya
题意:给你n个珠子,每个珠子有m种染色方法,将这些珠子做成长度为n的项链,问有多少种方法。两种方法是相同的当且仅当两种项链可以通过旋转或者翻转变成一种项链思路:题中有两种置换方式:1.旋转置换。分别顺时针旋转 i 个珠子,其循环节长度为 GCD(i,n)。(0<i<n);2.翻转置换。根据 N 的奇偶性分情况讨论。N为奇数时: 以第 i 个珠子为...原创 2018-05-18 22:36:00 · 250 阅读 · 0 评论 -
HDU - 5724 Chess——SG函数
题意:给你一个n*20的棋盘,每一行都有一些棋子,每次你可以把一个棋子移动到他右面的空位中离他最近的那一个,不能移动者输,Alice先手,问Alice能否取胜思路:题目叙述满足SG函数的使用条件,因此考虑使用SG函数解决问题大体思路是求出每一行的状态对应的SG值,最后把所有行的SG值进行异或运算,最终结果如果不等于0,则输出YES,否则输出NO如何设计SG函数呢?每一行的状态可...原创 2018-05-14 22:42:25 · 218 阅读 · 0 评论 -
HDU 2588 GCD——欧拉函数
题意:输入 N 和 M (2<=N<=1000000000,1<=M<=N),找出所有的X满足1<=X<=N且 gcd(X,N)>=M.思路:首先,求出N的所有约数g[],然后枚举那些>=M 的公约数g[i],结果为 所有n/g[i] 的欧拉函数的值的和解释:若x>=M,且x是N的约数,故 gcd(x,N)=x>=M 令y=N...原创 2018-05-11 18:15:07 · 285 阅读 · 0 评论 -
2.POJ 2480 Longge's problem——积性函数
题意:给定一个n,求Σgcd(i,n)(1<=i<=n)思路:(1)gcd是积性函数,根据积性函数的性质,积性函数的也是积性函数,故Σgcd(i,n)是积性函数。(2)设f(n) = Σgcd(i,n),根据积性函数的性质,可以得出下面的式子:f(n)=f(p1^a1)*f(p2^a2)*f(p3^a3)*...*f(pk^ak)此时问题就转化为了求f(pi^a...原创 2018-05-12 00:18:45 · 224 阅读 · 0 评论 -
HDU - 3501 Calculation 2——欧拉函数
题意:求小于n且不与n互素的正整数的和思路:若已知m与n互质,则n-m也与n互质 那么,对于任何一个i与n互质,必然n-i也和n互质,所以Φ(n)必然是偶数(除了2)所以从1到N与N互质的数的和为Φ(n)*n/2(2虽然不满足Φ(2)%2=0,但满足这个公式) #include <cstdio>#include <cstring>#i...原创 2018-05-12 00:25:02 · 241 阅读 · 0 评论 -
HDU - 1907 John——反尼姆博弈
必胜局面:1各堆石子数目异或和不等于0,且存在石子数目大于1的石子堆2各堆石子数目异或和等于0,且不存在石子数目大于1的石子堆#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const i...原创 2018-05-13 14:58:17 · 222 阅读 · 0 评论