POJ 2167 Irrelevant Elements 质因数分解


Irrelevant Elements

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2231   Accepted: 550
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

Source

Northeastern Europe 2004

[Submit]   [Go Back]   [Status]  
[Discuss]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn=100100;

int n,m;
int pm[maxn],pr[maxn],pc[maxn],pn;
vector<int> ans;

void f(int x,int d)
{
    for(int i=0;i<pn;i++)
        while(x%pm[i]==0)
        {
            pc[i]+=d;
            x/=pm[i];
        }
}

bool check()
{
    for(int i=0;i<pn;i++)
    {
        if(pc[i]<pr[i]) return false;
    }
    return true;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans.clear(); pn=0;

        /**********/
        for(int i=2;i*i<=m;i++)
        {
            if(m%i==0)
            {
                pm[pn]=i;
                while(m%i==0)
                {
                    pr[pn]++;
                    m/=i;
                }
                pn++;
            }
        }
        if(m!=1)
        {
            pm[pn]=m;
            pr[pn++]=1;
        }
        /**********/
        n--;
        for(int i=1;i<=n;i++)
        {
            f(n-i+1,1);
            f(i,-1);
            if(check())
            {
                ans.push_back(i+1);
            }
        }

        printf("%d\n",ans.size());
        sort(ans.begin(),ans.end());
        for(int i=0,sz=ans.size();i<sz;i++)
        {
            printf("%d ",ans[i]);
        }
        putchar(10);
    }
    return 0;
}
时间: 2024-10-08 02:54:35

POJ 2167 Irrelevant Elements 质因数分解的相关文章

POJ 2429 long long 质因数分解

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16206   Accepted: 3008 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a

GCD &amp; LCM Inverse POJ 2429(Pollard Rho质因数分解)

原题 题目链接 题目分析 这道题用Pollard Rho算法不能交G++,会RE!!!先说一下整体思路,gcd指gcd(a,b),lcm指lcm(a,b).a=x*gcd,b=y*gcd,则x,y互质且有x*y=lcm/gcd,要使a+b最小,也就是x+y最小.这里可以看出我们要做的就是分解lcm/gcd的质因子,然后枚举找出最小的x+y,最后输出a*x,a*y.这里还需要注意一下,题目是要求先输出小的再输出大的.至于Pollard Rho算法这里不讲. 代码 1 #include <cstdi

poj 1365 Prime Land 质因数分解

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int primeNUM[] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,

UVa1635 - Irrelevant Elements(质因数分解)

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 b

POJ 1845 Sumdiv#质因数分解+二分

题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想:选定一个要进行比较的目标,在区间[l,r]之间不断二分,直到取到与目标相等的值. #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll

POJ2167 Irrelevant Elements

Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu 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 g

UVa 1635 - Irrelevant Elements-[分解质因数]

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 b

【BZOJ2227】【ZJOI2011】看电影 [组合数学][质因数分解]

看电影 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特殊,具体方式如下: 1. 电影院的座位共有K个,并被标号为1…K,每个人买完票后会被随机指定一个座位,具体来说是从1…K中等可能的随机选取一个正整数,设其为L.

Codevs 1313 质因数分解

1313 质因数分解 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 Input Description 输入只有一行,包含一个正整数 n. 输出描述 Output Description 输出只有一行,包含一个正整数p,即较大的那个质数. 样例输入 Sample Input 21 样例输出 Sample Output 7 #include<iostream> #include<cstdio> #include<cm