poj2167 Irrelevant Elements

原创 2016年08月30日 13:46:30

Description Young cryptoanalyst Georgie is investigating different
schemes of generating random integer numbers ranging from 0 to m - 1.
He thinks that standard random number generators are not good enough,
so he has invented his own scheme that is intended to bring more
randomness into the generated numbers. First, Georgie chooses n and
generates n random integer numbers ranging from 0 to m - 1. Let the
numbers generated be a1, a2, … , an. After that Georgie calculates
the sums of all pairs of adjacent numbers, and replaces the initial
array with the array of sums, thus getting n - 1 numbers: a1 + a2, a2
+ a3, … , an-1 + an. Then he applies the same procedure to the new array, getting n - 2 numbers. The procedure is repeated until only one
number is left. This number is then taken modulo m. That gives the
result of the generating procedure. Georgie has proudly presented this
scheme to his computer science teacher, but was pointed out that the
scheme has many drawbacks. One important drawback is the fact that the
result of the procedure sometimes does not even depend on some of the
initially generated numbers. For example, if n = 3 and m = 2, then the
result does not depend on a2. Now Georgie wants to investigate this
phenomenon. He calls the i-th element of the initial array irrelevant
if the result of the generating procedure does not depend on ai. He
considers various n and m and wonders which elements are irrelevant
for these parameters. Help him to find it out.

Input Input contains n and m (1 <= n <= 100 000, 2 <= m <= 109).

Output On the first line of the output print the number of irrelevant
elements of the initial array for given n and m. On the second line
print all such i that i-th element is irrelevant. Numbers on the
second line must be printed in the ascending order and must be
separated by spaces.

在纸上推一推可以发现,最后得到的每个元素的系数恰好是杨辉三角的第n行。另外很容易看出,一个元素是无关的,当且仅当他的系数是m的倍数。
于是问题就变成了求C(n-1,0),C(n-1,1)..C(n-1,n-1)中有多少是m的倍数。
很容易想到一种错误的做法,直接用C(n,k)=n!/(n-k)!k!在模m下计算。因为m不一定是质数,所以除数的逆并不一定存在。
可以利用递推式C(n,k)=C(n,k-1)*(n-k+1)/k进行计算,根据唯一分解定理,只要对每个m的质因数的指数进行加减即可。

#include<cstdio>
#include<cstring>
#include<cmath>
int m,n,a[1000010],p[1000010],now[1000010],ans[1000010];
int main()
{
    int i,j,k,x,y,z,tot=0,cnt=0;
    bool flag;
    scanf("%d%d",&n,&m);
    x=sqrt(m+0.5);
    for (i=2;i<=x;i++)
      if (m%i==0)
      {
        a[++tot]=i;
        while (m%i==0)
        {
            m/=i;
            p[tot]++;
        }
      }
    if (m>1)
    {
        a[++tot]=m;
        p[tot]=1;
    }
    for (i=1;i<n-1;i++)
    {
        x=n-i;
        y=i;
        for (j=1;j<=tot;j++)
          while (x%a[j]==0)
          {
            x/=a[j];
            now[j]++;
          }
        for (j=1;j<=tot;j++)
          while (y%a[j]==0)
          {
            y/=a[j];
            now[j]--;
          }
        flag=1;
        for (j=1;j<=tot;j++)
          if (p[j]>now[j])
          {
            flag=0;
            break;
          }
        if (flag) ans[++cnt]=i+1;
    }
    printf("%d\n",cnt);
    for (i=1;i<=cnt;i++)
      printf("%d%c",ans[i],i==cnt?'\n':' ');
}
版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。

UVa1635 - Irrelevant Elements(质因数分解)

Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ...
  • mowayao
  • mowayao
  • 2014年08月29日 20:46
  • 834

uva 1635 - Irrelevant Elements

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4510 ...
  • u014569598
  • u014569598
  • 2014年08月31日 09:44
  • 544

UVa 1635 - Irrelevant Elements

这个程序跑了2s, 用了C(n, k)=C()
  • u014435976
  • u014435976
  • 2014年08月12日 10:59
  • 613

POJ 2167 Irrelevant Elements

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32215 经过紫书的分析,已经将问题转化为求组合数C(n...
  • S031302306
  • S031302306
  • 2015年12月09日 16:28
  • 500

uva1635 Irrelevant Elements(唯一分解定理)

题目链接:点击打开链接 题意:给定n个数a1,a2····an,依次求出相邻两个数值和,将得到一个新数列,重复上述操作,最后结果将变为一个数,问这个数除以m的余数与那些数无关?例如n=3,m=2时,...
  • mengxingyuanlove
  • mengxingyuanlove
  • 2015年08月10日 11:06
  • 1416

UVA 1635 Irrelevant Elements

看到一个很大的数字的什么什么的余数就要想到唯一分解定律 #include #include #include #include #include #include using namespace st...
  • qq_34446253
  • qq_34446253
  • 2016年07月06日 11:16
  • 122

UVA 1635 Irrelevant Elements

这个题 需要优化。 否则会超时。 C(m,n) z
  • Shimmer_
  • Shimmer_
  • 2014年08月12日 18:22
  • 539

irrelevant Elements (组合数学)uva-1635

题目大意:   对于给定的n个数a1,a2,a3....an,一次求出相邻两个数之和,将得到一个新的数列。重复上述操作,最后结果将变成一个数。问这个数除以m的余数与那些数无关?例如n=3,m=2时,...
  • txgANG
  • txgANG
  • 2017年03月29日 10:15
  • 179

Irrelevant Elements UVA - 1635

Think: 1埃式筛法筛取素数 2唯一分解定理 3组合数公式(C(k, n) = ((n-k+1)/k)*C(k-1, n))vjudge题目链接以下为Accepted代码#include ...
  • BlessingXRY
  • BlessingXRY
  • 2017年05月27日 22:32
  • 135

UVa 1635 Irrelevant Elements

Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ...
  • c21c21
  • c21c21
  • 2015年06月12日 15:43
  • 84
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2167 Irrelevant Elements
举报原因:
原因补充:

(最多只允许输入30个字)