poj2167 Irrelevant Elements

Irrelevant Elements

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2396 Accepted: 590
Case Time Limit: 2000MS

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.

Sample Input
3 2

Sample Output
1
2


【分析】
对于一个数列,每次求出相邻两个数之和得到一个新数列。最
后结果将变成一个数。问这个数除以m的余数与原数列中哪些
项无关?(无关指该数的系数是m的倍数)

不难发现累加之后每一项的系数就是杨辉三角第n-1层的各个数字,杨辉三角也就是组合数。而我们不能用杨辉三角递推,因为会T,所以直接从C(n-1,0),C(n-1,1)…递推到C(n-1,n-1)。
递推关系式通过公式法来解决↓,大家可以自己推一下。
C(n,k)=C(n,k-1)*(n-k+1)/(k)
先将m用唯一分解定理分解为素数乘积,然后通过组合数递推公式O(1)增减素因子,判断是否能整除m。
(其实可以不用线性筛和快速读入,省点代码复杂度)


【代码】

//poj 2167 Irrelevant Elements
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1e5;
int n,m,tot,r;
int a[mxn+5],cnt[mxn+5],pri[mxn+5],lin[mxn+5],ans[mxn+5];
bool vis[mxn+5];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void shai()
{
    int i,j;
    fo(i,2,mxn)
    {
        if(!vis[i]) pri[++pri[0]]=i;
        fo(j,1,pri[0])
        {
            if(i*pri[j]>mxn) break;
            vis[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
}
inline void getm(int x)
{
    int i,j;
    for(i=1;i<=pri[0] && i<=x;i++)
    {
        if(x%pri[i]==0) a[++tot]=pri[i];
        while(x%pri[i]==0)
        {
            x/=pri[i];
            cnt[tot]++;
        }
        if(x==1) break;
    }
    if(x>1) a[++tot]=x,cnt[tot]++;
}
inline void add(int x,int v)
{
    int i,j;
    fo(i,1,tot)
    {
        while(x%a[i]==0)
        {
            x/=a[i];
            lin[i]+=v;
        }
    }
}
int main()
{
    int i,j;
    n=read();m=read();
    shai();
    getm(m);
    n--;
    fo(i,1,n)
    {
        add(i,-1);
        add(n-i+1,1);
        fo(j,1,tot)
          if(lin[j]<cnt[j]) break;
        if(j>tot) ans[++r]=i+1;
    }
    printf("%d\n",r);
    fo(i,1,r)
      printf("%d ",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值