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 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 a1a2,..., 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 + a2a2 + 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 file contains several datasets. Each datasets has n and m ( 1n100 000, 2m109)
in a single line.

Output

On the first line of the output for each dataset print the number of irrelevant elements of the initial array for given n and m.
On the second line print all such ithat 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

题意:整个式子的和可以  化简为  sigma (C(n-1,i-1)*ai)

思路:只要判断C(n-1,i-1)能否被 m整除即可。

做法是先分解m的质因数,然后计算1!~(n-1)!  包含m的质因数的个数

C(n-1,i-1) = (n-1)!/((i-1)!*(n-i)!)

只要判断 剩下的质因数的个数是否大于等于m的任一个质因数的个数即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxp = 40000+10;
const int maxn = 100000+10;
int n,m;
bool isPrime[maxp];
vector<int> ret,prime,hp,cnt,tmp;
vector<int> g[maxn];
void getPrime(){
    memset(isPrime,true,sizeof isPrime);
    for(int i = 2; i < maxp; i++){
        if(isPrime[i]){
            prime.push_back(i);
            for(int j = i+i;j < maxp; j += i){
                isPrime[j] = false;
            }
        }
    }
}
void getDigit(){
    int tk = m;
    for(int i = 0; i < prime.size() && prime[i]*prime[i] <= tk; i++){
        if(tk%prime[i]==0){
            int k = 0;
            hp.push_back(prime[i]);
            while(tk%prime[i]==0){
                tk /= prime[i];
                k++;
            }
            cnt.push_back(k);
        }
    }
    if(tk>1){
        hp.push_back(tk);
        cnt.push_back(1);
    }
}
void init(){
    cnt.clear();
    hp.clear();
    ret.clear();
    getDigit();
    for(int i = 0; i <= n-1; i++) g[i].clear();
    for(int i = 0; i <= n-1; i++) {
        for(int j = 0; j < hp.size(); j++){
            int d = 0,t = i;
            while(t) {
                d += t/hp[j];
                t /= hp[j];
            }
            g[i].push_back(d);
        }
    }
}
void solve(){
	bool miden = false;
    for(int i = 2; i <= (n-1)/2+1; i++){
        bool flag = true;
        for(int j = 0; j < hp.size(); j++){
            int d = g[n-1][j]-g[i-1][j]-g[n-i][j];
            if(d < cnt[j]){
                flag = false;
                break;
            }
        }
        if(flag) {
			ret.push_back(i);
			if(i==(n-1)/2+1 && (n&1)) miden = true;
        }
    }
    tmp.clear();
    tmp = ret;
    if(n&1){
    	int i;
    	if(miden) i = tmp.size()-2;
    	else i = tmp.size()-1;
        for(; i >= 0; i--){
            ret.push_back(n+1-tmp[i]);
        }
    }else{
        for(int i = tmp.size()-1; i >= 0; i--){
            ret.push_back(n+1-tmp[i]);
        }
    }
    printf("%d\n",ret.size());
    if(ret.size()){
        printf("%d",ret[0]);
        for(int i = 1; i < ret.size(); i++){
            printf(" %d",ret[i]);
        }

    }
    puts("");
}
int main(){
    //freopen("test.txt","r",stdin);
    getPrime();
    while(~scanf("%d%d",&n,&m)){
        init();
        solve();
    }
    return 0;
}
时间: 2024-12-21 20:02:00

UVa1635 - Irrelevant Elements(质因数分解)的相关文章

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

UVa1635 - Irrelevant Elements(组合数)

题意:整个式子的和可以  化简为  sigma (C(n-1,i-1)*ai) 思路:只要判断C(n-1,i-1)能否被 m整除即可. 做法是先分解m的质因数,然后计算1!~(n-1)!  包含m的质因数的个数 C(n-1,i-1) = (n-1)!/((i-1)!*(n-i)!) 只要判断 剩下的质因数的个数是否大于等于m的任一个质因数的个数即可 <pre name="code" class="cpp">#include<iostream>

UVa1635 - Irrelevant Elements

通过观察发现其规律符合杨辉三角 需要注意的是最后ai的系数是C(i-1,n-1) 那么,问题就可以变成判断C(0,n-1),C(1,n-1)....C(n-1,n-1)哪些是m的倍数 只需要计算出m的唯一分解式中各个素因子在C(i-1,n-1)中的指数即可完成判断 然而为了节省时间,实际上我们只需算出m的每一个素因子在C(i-1,n-1)项中  含有几个即可 即我们将c(i-1,n-1)依次除以m的每一个素因子,直到无法整出,即可得出该项素因子的个数 紫薯上给出一个公式C(k,n)=(n-k+1

uva1635 Irrelevant Elements(唯一分解定理)

题目链接:点击打开链接 题意:给定n个数a1,a2····an,依次求出相邻两个数值和,将得到一个新数列,重复上述操作,最后结果将变为一个数,问这个数除以m的余数与那些数无关?例如n=3,m=2时,第一次得到a1+a2,a2+a3,在求和得到a1+2*a2+a3,它除以2的余数和a2无关.1=<n<=10^5, 2=<m<=10^9 解题思路: 1.首先我们可以发现对于给定的n其实每项的系数就是C(n-1,i-1),所以我们只需要找到每项的系数对m取余是否为0即可 2.由于m的取值

组合数杨辉三角(Irrelevant Elements uva1635)

Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbersranging from 0 to m - 1. He thinks that standard random number generators are not good enough, sohe has invented his own scheme that is intended to bri

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