【UVa1635】Irrelevant Elements - 唯一分解定理
题意
给你 \(n\) 个数,每次求出相邻两个数的和组成新数列。经过 \(n-1\) 次操作后,得到一个数。求这个数 \(mod \ m\) 与哪些项无关。
如:当 \(m=2 \ , \ n=2\) 时 \(a_1 \ , \ a_2 , a_3 \Rightarrow a_1+a_2 \ , \ a_2+a_3 \Rightarrow \ a_1+2a_2+a_3\) 则与 \(a_2\) 无关
思路
由二项式定理知道结果系数是杨辉三角的第 \(n-1\) 行,问题转换成判断有多少个 \(C_{n-1}^{i}\) 可以整除 \(m\)。
考虑 \(m\) 与 \(C_{n-1}^{i}\) 的唯一分解,\(\prod_{i=1}^n fac_i^{index_i}\) 与 \(C_{n-1}^{i}\) 作比较,当所有的质因子都在 \(C_{n-1}^{i}\) 中出现并且次数都小于 \(C_{n-1}^{i}\) 的次数时,即可整除。
分解 \(C_{n}^{i}\) 时需要用到组合数的递推式:\(C_{n}^{i}=\frac{n-i+1}{i} \times C_{n}^{i-1}\)。分解时只考虑 \(\frac{n-i+1}{i}\),因为 \(C_{n}^{i-1}\) 在上一次中计算过了。
/************************************************
*Author : lrj124
*Created Time : 2019.08.09.21:03
*Mail : 1584634848@qq.com
*Problem : uva1635
************************************************/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n,m,m_index[maxn],factor[maxn],cnt,c_index[maxn],ans[maxn];
inline void init() {
for (int i = 2;i*i <= m;i++)
if (!(m%i)) {
factor[++cnt] = i;
for (;!(m%i);m /= i,m_index[cnt]++);
}
if (m > 1) {
factor[++cnt] = m;
m_index[cnt]++;
}
}
inline bool check(int N,int k) {
N = N-k+1;
for (int i = 1;i <= cnt;i++) {
for (;!(N%factor[i]);N /= factor[i],c_index[i]++);
for (;!(k%factor[i]);k /= factor[i],c_index[i]--);
}
for (int i = 1;i <= cnt;i++)
if (m_index[i] > c_index[i]) return false;
return true;
}
int main() {
//freopen("uva1635.in","r",stdin);
//freopen("uva1635.out","w",stdout);
while (cin >> n >> m) {
memset(m_index,0,sizeof(m_index));
memset(c_index,0,sizeof(c_index));
ans[0] = cnt = 0;
init();
for (int i = 1;i <= n-2;i++)
if (check(n-1,i)) ans[++ans[0]] = i+1;
printf("%d\n",ans[0]);
for (int i = 1;i <= ans[0];i++) printf("%s%d",i ^ 1 ? " " : "",ans[i]);
printf("\n");
}
return 0;
}
【UVa1635】Irrelevant Elements - 唯一分解定理的更多相关文章
- UVA1635 Irrelevant Elements —— 唯一分解定理 + 二项式定理
题目链接:https://vjudge.net/problem/UVA-1635 (紫书320) 题解: 1.根据二项式定理, 可得递推公式: C(n,k) = (n-k+1)/k * C(n, k- ...
- UVA1635 Irrelevant Elements(唯一分解定理 + 组合数递推)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51196 紫书P320; 题意:给定n个数a1,a2····an,依次求出相邻 ...
- POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2407 Accepted: 59 ...
- UVa1635 - Irrelevant Elements
通过观察发现其规律符合杨辉三角 需要注意的是最后ai的系数是C(i-1,n-1) 那么,问题就可以变成判断C(0,n-1),C(1,n-1)....C(n-1,n-1)哪些是m的倍数 只需要计算出m的 ...
- Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理
/** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...
- 【组合数的唯一分解定理】Uva1635
给出n.m,求得最终求和数列an=C(n-1,0)*x1 + C(n-1,1)*x2+...+C(n-1,n-1)*xn; 若xi与m无关,则an除以m的余数与xi无关,即余数不含xi的项: 输入:n ...
- Irrelevant Elements UVA-1635 (二项式定理)
vjudge链接 原题链接 乍一看似乎没什么思路,但是写几个简单的例子之后规律就变得很明显. 比如当 n=5 时,每一步计算后的结果如下: a1 a1+a2 a1+2a2+a3 a1+3a2+3a3+ ...
- poj2773 —— 二分 + 容斥原理 + 唯一分解定理
题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板
You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...
随机推荐
- Notion笔记工具免费开通教育许可
修改为edu邮箱 如果咱注册的时候就用的咱的edu,就不用看这部分啦! 点击[Get free Education plan],提示要修改咱的注册邮箱! 开通咱的教育版 最后附上ac邮箱两枚 http ...
- 细说JavaScript 导出 上万条Excel数据
首先这是个鸡肋的方法 合理的做法是 后端直接生成 前端给个链接就行了 (先说原因与过程最后上代码) 1. 先说说为什么会出现这个需求吧. : 在写运维网站时 自己粗略的看了一下bootstarp-ta ...
- C#递归的简单实例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 两个问题解答、opencv、tensorflow、numpy、matplotlib的基本使用
开始图像处理的海洋 (1)两个问题的详细解答 在开始畅游opencv.tensorflow的海洋之前,我们这里先要解决两个问题. 1.Jupyter notebook 解决了无法自动跳转到浏览器的问题 ...
- IO—》递归
递归的概述 递归,指在当前方法内调用自己的这种现象 public void method(){ System.out.println(“递归的演示”); //在当前方法内调用自己 method(); ...
- 集合的一些实例的demo实现
按照斗地主的规则,完成洗牌发牌的动作. 具体规则: 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌. 准备牌: 牌可以设计为一个ArrayList,每个字符串为一 ...
- 从RNN到BERT
一.文本特征编码 1. 标量编码 美国:1 中国:2 印度:3 … 朝鲜:197 标量编码问题:美国 + 中国 = 3 = 印度 2. One-hot编码 美国:[1,0,0,0,…,0]中国:[0, ...
- statsmodels 示例
Statsmodels 示例 https://www.statsmodels.org/stable/examples/index.html
- Python while 中简单的语句组
Python while 中简单的语句组: 只使用 while: # 简单的语句组 a = 4 b = 8 num = 0 while a < b: print("a 比 b 小&qu ...
- PHP natsort() 函数
------------恢复内容开始------------ 实例 对数组进行排序: <?php$temp_files = array("temp15.txt"," ...