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 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 andm 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 (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 109) 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 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

解题思路:

  分解质因数只用筛10^5以内的素数即可,别忘了将大于10^5的质因子另外存储。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <ctime>
 8 using namespace std;
 9 #define maxn 100010
10 #define time_ printf("%f",double(clock())/CLOCKS_PER_SEC)
11 int vis[maxn];
12 vector<int> prime;
13 vector<int> fm;
14 int e_m[maxn];
15 int e_c[maxn];
16 int n,m;
17 void pre(){
18     int m=sqrt(maxn)+1;
19     for(int i=2;i<m;i++){
20         if(!vis[i]){
21             for(int j=i*i;j<maxn;j+=i){
22                 vis[j]=1;
23             }
24         }
25     }
26     for(int i=2;i<maxn;i++)
27         if(!vis[i]) prime.push_back(i);
28 }
29 void cal_e(int m,int e_m[maxn],int d){
30     int t=m;
31     for(int i=0;i<fm.size();i++){
32         while(t%fm[i]==0){
33             e_m[i]+=d;
34             t/=fm[i];
35         }
36         if(t==1)break;
37     }
38 }
39 bool judge(){
40     for(int i=0;i<fm.size();i++)
41         if(e_m[i]>e_c[i]) return false;
42     return true;
43 }
44 int main(int argc, const char * argv[]) {
45     pre();
46     while(scanf("%d%d",&n,&m)==2){
47         memset(e_m,0,sizeof e_m);
48         memset(e_c,0,sizeof e_c);
49         fm.clear();
50         cal_e(m,e_m,1);
51
52         vector<int> ans;
53
54         int t=m;
55         int j=0;
56         for(int i=0;i<prime.size();i++){
57             if(t%prime[i]==0){
58                 fm.push_back(prime[i]);
59                 while(t%prime[i]==0){
60                     e_m[j]++;
61                     t/=prime[i];
62                 }
63                 j++;
64             }
65             if(t==1)break;
66         }
67         if(t!=1) {fm.push_back(t);e_m[j]=1;}
68         for(int k=1;k<n;k++){
69             cal_e(n-k,e_c,1);
70             cal_e(k,e_c,-1);
71             if(judge()){
72                 ans.push_back(k);
73             }
74         }
75         printf("%d\n",(int)ans.size());
76         if(ans.size()>0){
77             printf("%d",ans[0]+1);
78             for(int i=1;i<ans.size();i++)
79                 printf(" %d",ans[i]+1);
80         }
81         //else printf("\n");
82         printf("\n");
83         //time_;
84     }
85
86     return 0;
87 }
时间: 2024-11-08 23:13:55

UVa 1635 - Irrelevant Elements-[分解质因数]的相关文章

uva 1635 - Irrelevant Elements

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=850&problem=4510 题意:一组数字a1,a2,a3.......,不断的相邻数成一组求和形成新的一组,直到最后不可以在合并为止,问最后的答案对m取模与哪些初始的项的无关? 题解:显然无关就是要求某一项在最终的数列中的系数是m的倍数,并且显然最终系列的系数是二项分布

UVa 1635 Irrelevant Elements (唯一分解定理 || 组合数学)

题目 题目大意 对于给定的\(n\)个数\(a_1\), \(a_2\), ···, \(a_n\), 依次求出相邻两数之和, 将得到一个新数列.重复上述操作, 最后结果将变成一个数.问这个数除以\(m\)的余数将与哪些数无关? 例如\(n = 3\), \(m = 2\)时, 第一次求和得到\(a_1 + a_2\), \(a_2 + a_3\), 再求和得到\(a_1 + 2a_2 + a_3\), 它除以\(2\)的余数和\(a_2\)无关.\(1 ≤ n ≤ 10^5\), \(2 ≤

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 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

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

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a

hdoj-1164-Eddy&#39;s research I【分解质因数】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7537 Accepted Submission(s): 4579 Problem Description Eddy's interest is very extensive, recently he is interested in prime number

分解质因数模板

/*==================================================*| 分解质因数,可能有些地方需要改为long long \*==================================================*/ const int MAXN=100010; int prm[MAXN+1]; bool is[MAXN+1]; int getprm(int n){ int i, j, k = 0; int s, e = (int)(sqrt

java编程题 --分解质因数

package Solve; import java.util.Scanner; public class Solve { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入一个正整数:"); int num = scan.nextInt(); System.out.print(num + " = "