UVA-1635

题意:

给定n个数a1,a2,a3.....an,依次求出相邻的两个数的和,最后成为一个数,问这个数模m的值与那些最初的数无关

例:a1,a2,a3, m=2 => a1+a2,a2+a3 => a1+2a2+a3 ,显然第二项无关。

代码:

//可以看出最后得到的式子符合杨辉三角,所以可以递推出所有的项的系数(c(i+1)=c(i)*(n-i)/(i+1)),
//但是数太大不能直接计算,可以将他们分解成 a1^p1*a2^p2*... 的形式,这样就只需要判断因子的指数的大小即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=100000;
int c[maxn+10];
int ans[maxn+10],prime[maxn+10],re[maxn+10],tot;
void init(int m,int p){tot=0;memset(re,0,sizeof(re));for(int i=2;i<=sqrt(m+0.5);i++){if(m%i==0){prime[++tot]=i;while(m%i==0){m/=i;re[tot]+=p;}}}if(m>1){prime[++tot]=m;re[tot]+=p;}
}
int solve(int m,int p){for(int i=2;i<=sqrt(m+0.5);i++){if(m%i==0){while(m%i==0){m/=i;c[i]+=p;}}}if(m>1) c[m]+=p;for(int i=1;i<=tot;i++){if(prime[i]>maxn) return 0;else if(c[prime[i]]<re[i])return 0;}return 1;
}
int main()
{int n,m;while(scanf("%d%d",&n,&m)==2){init(m,1);int cnt=0;memset(c,0,sizeof(c));n--;for(int i=0;i<n;i++){solve(n-i,1);int tmp=solve(i+1,-1);if(tmp) ans[++cnt]=i+2;}printf("%d\n",cnt);for(int i=1;i<=cnt;i++)printf("%d%c",ans[i],i==cnt?'\n':' ');if(cnt==0) printf("\n");//坑!!!
    }return 0;
}

转载于:https://www.cnblogs.com/--ZHIYUAN/p/7356031.html

UVA-1635 数学相关推荐

  1. Candy UVA 1639 (数学期望)

    Candy UVA 1639 (数学期望) 有两个盒子各有n颗糖(n<=2*105),从两个盒子取糖的概率为p和1-p,直到一个盒子没糖,求另一个盒子中糖的个数的数学期望. 期望公式为: E=∑ ...

  2. UVA - 11181 数学

    UVA - 11181 题意: n个人去买东西,其中第i个人买东西的概率是p[i],最后只有r个人买了东西,求每个人实际买了东西的概率 代码: //在r个人买东西的概率下每个人买了东西的概率,这是条件 ...

  3. uva 11762 数学期望+记忆化搜索

    题目大意:给一个正整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/p,否则N不变,问平均情况下需要多少次随机选择,才能把N变成1? 分析:根据数学期望的线性和全期望公 ...

  4. UVa 1635 无关的元素(唯一分解定理+二项式定理)

    https://vjudge.net/problem/UVA-1635 题意: 给定n个数a1,a2,...an,依次求出相邻两数之和,将得到一个新数列.重复上述操作,最后结果将变成一个数.问这个数除 ...

  5. uva 11021 数学概率 麻球

    GRAVITATION, n. "The tendency of all bodies to approach one another with a strength proportion ...

  6. uva 10883——Supermean

    Do you know how to compute the mean (or average) of n numbers? Well, that's not good enough for me. ...

  7. Probability|Given UVA - 11181

    Probability|Given UVA - 11181 数学·概率 http://www.cnblogs.com/staginner/archive/2011/12/13/2286032.html ...

  8. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  9. c语言指针倒数之和,用C语言将一个数开根号后再取倒数的方法

    在上学的时候,曾经看过有人写过这样的算法,就是将一个数开根号后再取倒数的算法,我本人也觉得十分巧妙,于是就将它积累了下来,让我们来看看是怎么回事: #include #include float my ...

  10. UVa 1639 - Candy(数学期望 + 精度处理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

最新文章

  1. 代码编译delphi条件编译
  2. Vitaly and Night
  3. 简书 android底部导航,使用recyclerView封装底部导航栏
  4. vue_过滤器_note
  5. LayuI固定块关闭
  6. paip.gch预编译头不生效的原因以及解决:
  7. the volume for a file has been externally altered so that the opened file is no longer valid
  8. 公众号丶服务号丶订阅号三者间的区别
  9. H2O机器学习:一种强大的可扩展的人工智能和深度学习技术
  10. Unity3D —— 手机设备震动接口
  11. KMeans算法的Mapreduce实现
  12. chrome用 --proxy-server 单独设置代理
  13. vue element-ui 实现可输入的选择下拉框
  14. Verilog中的Signed Decimal与Signed Magnitude区别
  15. 图解Ubuntu分区
  16. 四十六、基础框架(四)字符串物件
  17. 单片机:c语言实现秒表计数(按键开始,结束,重置)
  18. FAST2022 DEPART: Replica Decoupling for Distributed Key-Value Storage Qiang(翻译分析)
  19. 一款运动类APP开发需要哪些功能
  20. 多疗程40Hz tACS对阿尔茨海默病患者海马灌注的影响

热门文章

  1. java byte 转换int_java byte负数转换int失真?
  2. Coregistration: Simultaneous Alignment and Modeling of Articulated 3D Shape
  3. 3Dshader之球形环境映射(sphere environment mapping)
  4. 通常情况下登录linux桌面环境需要,在通常情况下,登录Linux桌面环境,需要。...
  5. ros melodic控制真实机械臂之获取moveit规划插补点
  6. python简单数据类型变量_python-2-变量和简单数据类型
  7. 文本生成,文本数据增强怎么做?
  8. oracle bookauthor,Oracle 聚簇(征集)
  9. html5 input选择文件,input文件选择,限定文件类型。
  10. python text insert()背景色_50行python代码写个计算器教程