uva-1635-Irrelevant Elements-唯一分解定理,组合数

来源:互联网 发布:python脚本服务器部署 编辑:程序博客网 时间:2024/05/16 05:25

F - Irrelevant Elements
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu
SubmitStatusPracticeUVA 1635
Appoint description:

Description

Download as PDF Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ranging from0 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 from0 to m - 1. Let the numbers generated bea1, 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 gettingn - 1 numbers:  a1 +a2, a2 + a3,..., an - 1 + an. Then he applies the same procedure to the new array, gettingn - 2 numbers. The procedure is repeated until only one number is left. This number is then taken modulom. 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, ifn = 3 and m = 2, then the result does not depend ona2. Now Georgie wants to investigate this phenomenon. He calls thei-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 andm ( 1$ \le$n$ \le$100 000,2$ \le$m$ \le$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 givenn and m. On the second line print all suchi 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 

12


紫书例题。


#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<vector>using namespace std;int n, m;int cnt;bool ok;vector<int> q, e;vector<int> ans;void add_integer(){       int len=sqrt(m)+1;       for(int i=2;i<=len;i++){              if(m%i==0) {                     e.push_back(0);                     while(m%i==0){                            e[e.size()-1]++;                            m/=i;                     }                     q.push_back(i);                     //m/=i;                     if(!m) return ;              }       }       if(m!=1) {              e.push_back(1);              q.push_back(m);       }}bool judge(int k, int p){       int len= q.size();       bool ok=true;       for(int i=0;i<len;i++){              while(k&&k%q[i]==0){                     e[i]-=p;                     k/=q[i];              }              if(ok&&e[i]>0) ok=false;       }       if(ok) return true;       return false;}void solve(){       q.clear();       e.clear();       ans.clear();       add_integer();       ok=false;       cnt=0;       for(int i=1;i<n;i++){              judge(n-i+1, 1);              if(judge(i, -1) ){                     cnt++;                     ans.push_back(i+1);              }       }}int main(){       while(cin>>n>>m){              n-=1;              //cout<<++kase<<endl;              solve();              int len = ans.size();              cout<<cnt<<endl;              for(int i=0;i<len ;i++){                     if(!i) cout<<ans[i];                     else cout<<" "<<ans[i];              }              cout<<endl;       }       return 0;}


0 0