您的位置:首页 > 其它

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

2015-05-19 20:19 555 查看
F - Irrelevant Elements
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld
& %llu
SubmitStatusPracticeUVA
1635

Appoint description:

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


紫书例题。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: