数论四之综合训练——Magic Pairs,Crime Management,Top Secret,组合数问题

数论综合训练

  • Magic Pairs
    • problem
    • solution
    • code
  • CF107D Crime Management
    • problem
    • solution
    • code
  • UVA12183 Top Secret
    • problem
    • solution
    • code
  • P3746 [六省联考2017]组合数问题
    • problem
    • solution
    • code

Magic Pairs

problem

已知A0x+B0y≡0(modn)A_0x+B_0y\equiv 0\pmod nA0x+B0y0(modn)恒成立

求所有满足Ax+By≡0(modn)Ax+By\equiv 0\pmod nAx+By0(modn)成立的(A,B)(A,B)(A,B)数量,0≤A,B<N0\le A,B<N0A,B<N

solution

既然A0x+B0y≡0(modn)A_0x+B_0y\equiv 0\pmod nA0x+B0y0(modn)都已经成立了

那么2A0x+2B0y≡0(modn)2A_0x+2B_0y\equiv 0\pmod n2A0x+2B0y0(modn)​也同样成立

换言之kA0x+kB0y≡0(modn)kA_0x+kB_0y\equiv 0\pmod nkA0x+kB0y0(modn)​​都成立

无脑倍加,还对A,BA,BA,B划了范围,肯定是会有周期循环的

注意A0,B0A_0,B_0A0,B0要先模一下nnn

code

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector < pair < int, int > > ans;int main() {int n, a0, b0;scanf( "%d %d %d", &n, &a0, &b0 );a0 %= n, b0 %= n;int a = a0, b = b0;do {ans.push_back( make_pair( a, b ) );a = ( a + a0 ) % n;b = ( b + b0 ) % n;}while( a != a0 || b != b0 );sort( ans.begin(), ans.end() );printf( "%d\n", ans.size() );for( int i = 0;i < ans.size();i ++ )printf( "%d %d\n", ans[i].first, ans[i].second );return 0;
}

CF107D Crime Management

problem

solution

limi:ilim_i:ilimi:i字符所有限制之积

所有cnt相乘小于等于123,换言之:如果枚举每个字符iii的出现次数取模limilim_ilimi,哈希的总状态数不超过123123123

取模后的状态转移是固定的,现在iii出现了jjj次,那么下一个状态(j+1)%limi(j+1)\%lim_i(j+1)%limi就可以设为111​(可以转移过去)

与长度无关,因此可以矩阵加速

但最后第一行的哪些答案是符合条件的,就还需要一次dfs,确定对于每一个iii枚举的长度都必须至少是一个限制的倍数

到最后一层的时候,就去调用哈希字符串对应的编号,在矩阵中进行查询,累加到答案上

code

#include <map>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define mod 12345
#define int long long
vector < int > G[27];
int n, m, cnt, ret;
int lim[27];struct matrix {int c[125][125];matrix() { memset( c, 0, sizeof( c ) ); }int * operator [] ( int i ) { return c[i]; }matrix operator * ( matrix &t ) const {matrix ans;for( int i = 1;i <= 123;i ++ )for( int j = 1;j <= 123;j ++ )for( int k = 1;k <= 123;k ++ )ans[i][j] = ( ans[i][j] + c[i][k] * t[k][j] ) % mod;return ans;}
}g;matrix qkpow( matrix x, int y ) {matrix ans;for( int i = 1;i <= 123;i ++ )ans[i][i] = 1;while( y ) {if( y & 1 ) ans = ans * x;x = x * x;y >>= 1;}return ans;
}struct node {int cnt[27];node() { memset( cnt, 0, sizeof( cnt ) ); }bool operator < ( const node &t ) const {for( int i = 1;i <= 26;i ++ )if( cnt[i] == t.cnt[i] ) continue;else return cnt[i] < t.cnt[i];return 0;}
}h;map < node, int > mp;void dfs( int x, node s ) {if( x > 26 ) {mp[s] = ++ cnt;return;}if( ! lim[x] ) dfs( x + 1, s );for( int i = 0;i < lim[x];i ++ ) {s.cnt[x] = i;dfs( x + 1, s );}
}void calc( int x, node s ) {if( x > 26 ) {ret = ( ret + g[1][mp[s]] ) % mod;return;}if( ! lim[x] ) calc( x + 1, s );for( int i = 0;i < lim[x];i ++ ) {for( auto j : G[x] )if( i % j == 0 ) goto next;continue;next :s.cnt[x] = i;calc( x + 1, s );}
}signed main() {scanf( "%lld %lld", &n, &m );if( ! n ) return ! printf( "1\n" );if( ! m ) return ! printf( "0\n" );for( int i = 1;i <= m;i ++ ) {char ch; int x, c;scanf( "\n%c %lld", &ch, &x );c = ch - 'A' + 1;if( ! lim[c] ) lim[c] = x;else lim[c] *= x;G[c].push_back( x ); }dfs( 1, h );for( map < node, int > :: iterator it = mp.begin();it != mp.end();it ++ ) {int id = it -> second; node now = it -> first;for( int i = 1;i <= 26;i ++ ) {if( ! lim[i] ) continue;node t = now;t.cnt[i] = ( now.cnt[i] + 1 ) % lim[i];g[id][mp[t]] ++;}}g = qkpow( g, n );calc( 1, h );printf( "%lld\n", ret );return 0;
}

UVA12183 Top Secret

problem

NNN个数排成一圈,一次操作为,每个位置的数+=L×+=L\times+=L×+R×+R\times+R×右,保留xxx为整数

SSS轮操作后每个位置的值

N<=1000,S<=230,x<=9N<=1000,S<=2^{30},x<=9N<=1000,S<=230,x<=9

solution

非常标准的矩乘转移加速题干

但是发现nnn​级别在1e31e31e3​,矩乘是n3n^3n3的复杂度,也就是说常规的矩乘不足以通过这道题

发现转移矩阵的每一行相当于上一行进行右移111位,这种特殊的矩阵称之为循环矩阵

只需要计算第一行的加速矩阵,然后每一行都可以根据一定的公示在第一行进行查找

code

#include <cstdio>
#include <cstring>
#define maxn 1005
#define int long long
int n, s, l, r, x, mod, T;
int ret[maxn], h[maxn];struct matrix {int n;int c[2][maxn];void clear() { memset( c, 0, sizeof( c ) ); }matrix() { clear(); }int * operator [] ( int i ) { return c[i]; }matrix operator * ( matrix &t ) const {matrix ans; ans.n = n;for( int k = 1;k <= n;k ++ )for( int j = 1;j <= n;j ++ ) {int i = ( j - k + 1 + n ) % n;if( ! i ) i = n;ans[1][j] = ( ans[1][j] + c[1][k] * t[1][i] ) % mod;}return ans;}
}g;matrix qkpow( matrix x, int y ) {matrix ans; ans.n = x.n; ans[1][1] = 1;while( y ) {if( y & 1 ) ans = ans * x;x = x * x;y >>= 1;}return ans;
}signed main() {scanf( "%lld", &T );while( T -- ) {scanf( "%lld %lld %lld %lld %lld", &n, &s, &l, &r, &x );mod = 1;for( int i = 1;i <= x;i ++ )mod = ( mod << 3 ) + ( mod << 1 );g.clear(); g.n = n;for( int i = 1;i <= n;i ++ )scanf( "%lld", &h[i] ), ret[i] = 0;g[1][1] = 1, g[1][n] = l, g[1][2] = r;g = qkpow( g, s );for( int k = 1;k <= n;k ++ )for( int i = 1;i <= n;i ++ ) {int j = ( k - i + 1 + n ) % n;if( ! j ) j = n;ret[i] = ( ret[i] + g[1][j] * h[k] ) % mod;}for( int i = 1;i < n;i ++ )printf( "%lld ", ret[i] );printf( "%lld\n", ret[n] );}return 0;
}

P3746 [六省联考2017]组合数问题

problem

solution

fi,j:f_{i,j}:fi,j:iii个物品选择物品数量取模kkkjjj的方案数

fi,j=fi−1,j+fi−1,(j−1+k)%kf_{i,j}=f_{i-1,j}+f_{i-1,(j-1+k)\%k}fi,j=fi1,j+fi1,(j1+k)%k

iii的转移只跟i−1i-1i1有关,且jjj的转移也是固定的

可以矩乘加速

code

#include <cstdio>
#include <cstring>
using namespace std;
#define int long long
#define maxn 50
int n, p, K, r;
struct matrix {int c[maxn][maxn];matrix() {memset( c, 0, sizeof( c ) );}int * operator [] ( int i ) {return c[i];}matrix operator * ( matrix &t ) const {matrix ans;for( int i = 0;i < K;i ++ )for( int j = 0;j < K;j ++ )for( int k = 0;k < K;k ++ )ans[i][j] = ( ans[i][j] + c[i][k] * t[k][j] ) % p;return ans;} 
}ret, g;matrix qkpow( matrix x, int y ) {matrix ans;for( int i = 0;i < K;i ++ )ans[i][i] = 1;while( y ) {if( y & 1 ) ans = ans * x;x = x * x;y >>= 1;}return ans;
}signed main() {scanf( "%lld %lld %lld %lld", &n, &p, &K, &r );ret[0][0] = 1;for( int i = 0;i < K;i ++ )g[i][i] ++, g[i][( i - 1 + K ) % K] ++;g = qkpow( g, n * K );ret = ret * g;printf( "%lld\n", ret[0][r] );return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/317305.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

P6776-[NOI2020]超现实树

正题 题目链接:https://www.luogu.com.cn/problem/P6776 题目大意 定义一次操作为将一棵树的叶子换成另一棵树。 定义一棵树TTT的grow(T)grow(T)grow(T)表示所有树TTT能够通过操作变成的树的集合。 现在给出mmm棵树TiT_iTi​&#xff0c;定义SSS为所有grow(Ti)grow(T_i)grow…

CodeForces:749(div1)750(div2)

文章目录前言CF450A Jzzhu and ChildrenDescription\text{Description}DescriptionSolution\text{Solution}SolutionCode\text{Code}CodeCF450B Jzzhu and SequencesDescription\text{Description}DescriptionSolution\text{Solution}SolutionCode\text{Code}CodeCF449A Jzzhu …

Steins;Game Gym - 102798J(未解决)

Steins;Game Gym - 102798J 题意&#xff1a; 给定n堆石子a&#xff0c;每堆石子被染成了黑色或者白色&#xff0c;现在两个人轮流进行以下的其中一个操作&#xff1a; 1、从石子数量最少的一个黑色石堆中拿走若干石子 2、从任意一个白色石堆中拿走若干石子 两个人都采取最优…

ML.NET 0.10特性简介

IDataView被单独作为一个类库包IDataView组件为表格式数据提供了非常高效的处理方式&#xff0c;尤其是用于机器学习和高级分析应用。它被设计为可以高效地处理高维数据和大型数据集。并且也适合处理属于更大的分布式数据集中的单个数据区块结点。在ML.NET 0.10中&#xff0c;I…

模板:模拟退火

文章目录前言解析流程示例代码trick所谓模拟退火&#xff0c;就是通过代码模拟退火 &#xff08;逃&#xff09; 前言 终于学了这个神奇的骗分算法 几次在大赛中都发现这算法是真的有学的必要 FFC可能真的要想想自己的题目对OI界的导向作用了 但学完以后还是感觉挺有意思的&a…

讨“动态规划“檄

我一直遵循着人不犯我我不犯人的原则&#xff0c;但是鉴于动态规划(dp)三番两次的挑衅&#xff0c;我忍无可忍决定发起反击&#xff0c;特写本文记录一下。 (本文整理一下以前做过的dp问题&#xff0c;并且把之前囤的大量dp都做做) acwing提高组 第一章 动态规划 背包模型题目集…

数论五之容斥——硬币购物,Gerald and Giant Chess,幸运数字,Sky Full of Stars,已经没有什么好害怕的了

容斥的神[HAOI2008]硬币购物problemsolutioncodeCF559C Gerald and Giant Chessproblemsolutioncode[SCOI2010]幸运数字problemsolutioncodeCF997C Sky Full of Starsproblemsolutioncode已经没有什么好害怕的了problemsolutioncode[JLOI2015]骗我呢problemsolutioncode容斥要么…

P4769-[NOI2018]冒泡排序【组合数学,树状数组】

正题 题目链接:https://www.luogu.com.cn/problem/P4769 题目大意 有一个冒泡排序的算法 输入&#xff1a;一个长度为 n 的排列 p[1...n] 输出&#xff1a;p 排序后的结果。 for i 1 to n dofor j 1 to n - 1 doif(p[j] > p[j 1])交换 p[j] 与 p[j 1] 的值然后给出一…

NET Core微服务之路:基于Ocelot的API网关Relay实现--RPC篇

前言我们都知道&#xff0c;API网关是工作在应用层上网关程序&#xff0c;为何要这样设计呢&#xff0c;而不是将网关程序直接工作在传输层、或者网络层等等更底层的环境呢&#xff1f;让我们先来简单的了解一下TCP/IP的五层模型。&#xff08;图片出自http://www.cnblogs.com/…

模板:长链剖分

所谓长链剖分&#xff0c;就是对长链进行剖分 &#xff08;逃&#xff09; 前言 很优雅的算法 利用对指针进行魔法操作将 n2n^2n2 的 dp 优化成线性 线性啊&#xff01;&#xff01;&#xff01; 解析 CF1009F Dominant Indices 给定一棵以 111 为根&#xff0c;nnn 个节点…

acwing 327. 玉米田

327. 玉米田 题意&#xff1a; m * n的土地&#xff0c;有的土地不育&#xff0c;有的可以种植&#xff0c;要求相邻的土地不能同时种植玉米&#xff0c;问有多少种种植方式 题解&#xff1a; 状压dp&#xff0c;先存每一行可能的状态&#xff0c;然后状态转移&#xff0c;…

Windows 10《描图》应用现已开源

点击上方蓝字关注“汪宇杰博客”《描图》是我最早的Windows 10应用&#xff0c;发布至今已3年多&#xff0c;积累了全球数百万用户&#xff0c;广受好评。现已开源。这款应用为不少小朋友带去了欢乐&#xff0c;体验绘画的乐趣&#xff0c;也帮助过专业用户复刻数百幅古代绘画。…

数论六之计算几何干货——计算几何模板解释全集 及 模板检验训练场

文章目录点和向量及运算直线和线段求解点到直线的距离/点在直线上求解点到线段的距离/点在线段上求解两条线段是否相交求解两直线的交点多边形求解多边形面积求解多边形重心求解判断定点与多边形的位置关系凸包graham扫描法graham扫描法加强版圆求解圆与直线的交点求解圆与圆的…

P7740-[NOI2021]机器人游戏【dp,bitset】

正题 题目链接:https://www.luogu.com.cn/problem/P7740 题目描述 题目大意摸了 小 R 有 mmm&#xff08;1≤m≤10001 \le m \le 10001≤m≤1000&#xff09;个机器人和 mmm 张纸带&#xff0c;第 iii&#xff08;1≤i≤m1 \le i \le m1≤i≤m&#xff09;个机器人负责对第 …

CodeForces:54

文章目录前言CF54A PresentsDescription\text{Description}DescriptionSolution\text{Solution}SolutionCF54B Cutting Jigsaw PuzzleDescription\text{Description}DescriptionSolution\text{Solution}SolutionCode\text{Code}CodeCF54C First Digit LawDescription\text{Desc…

[NOIP2016 提高组] 愤怒的小鸟

[NOIP2016 提高组] 愤怒的小鸟 题意&#xff1a; 有n只猪&#xff0c;给出猪的坐标(xi,yi),问最少用几个形如 yax^2bx 的曲线可以保证所有猪在曲线上&#xff0c;满足a<0,a,b为实数 n<18, 题解&#xff1a; 两个方法&#xff1a;爆搜或者状压dp 状压dp 看n<18也…

P5208-[WC2019] I 君的商店【交互,二分】

正题 题目链接:https://www.luogu.com.cn/problem/P5208 题目大意 有一个长度为nnn的010101序列aaa&#xff0c;你知道里面有奇数个111还是偶数个111。你每次可以选择两个下标集合S/TS/TS/T询问集合SSS和集合TTT位置的数字和哪个更大。 交互库只会告诉你S≤TS\leq TS≤T或者…

模板:后缀数组(SA)

文章目录前言解析后缀排序优化1&#xff1a;基数排序优化2&#xff1a;简化第一次排序优化3&#xff1a;提前break完整代码LCP与height所谓后缀数组&#xff0c;就是存储后缀的数组 &#xff08;逃&#xff09; 前言 为什么一个算法&#xff0c;如此难以理解却依然是成为一个…

P3959 [NOIP2017 提高组] 宝藏

P3959 [NOIP2017 提高组] 宝藏 题意: 额题意不好说&#xff0c;就是n个点m个边&#xff0c;选定一个点为根节点&#xff0c;构造一个最小生成树&#xff0c;边的权值为该该边起点到根节点之间的点的数量K&#xff08;不含根节点&#xff09; * 道路长度 1<n<12 0<m&…

如何在ASP.NET Core程序启动时运行异步任务(3)

原文&#xff1a;Running async tasks on app startup in ASP.NET Core (Part 3)作者&#xff1a;Andrew Lock译者&#xff1a;Lamond Lu之前我写了两篇有关在ASP.NET Core中运行异步任务的博文&#xff0c;本篇博文是对之前两篇博文中演示示例和实现方法的简短跟进。你可以通过…