学到
1.如何得到一个数的因数,
2.对于二项式的算法
此题困扰了好久,1处是考虑欠缺,只想着快点减枝,忽略了每个数据是相关的,不可提前减枝。
第2处始终没有明白,为什么不可以使用c(i,n)=c(n-i,n);
就是
for(i=1;i<=a/2;i++)
{if(check(a-i,i)){anall[++ans]=i+1;if((i+1)!=(a-i))anall[++ans]=a-i;
}
ac代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int fau;
const int large=1e5+10;
int fac[large][2],num;
void divide(int b)
{num=0;
memset(fac,0,sizeof(fac));
for(int i=2;i<=sqrt(b);i++)
{if(b%i==0)
{ fac[++num][0]=i;
while(b%i==0)
{
fac[num][1]++;
b=b/i;
}
}
}
if(b>large)fau=1;
if(b>1){fac[++num][0]=b;fac[num][1]++;}
}
int all[large];
bool check(int a,int b)
{int i;
a=a-b;
for( i=1;i<=num;i++)
{ while(a%fac[i][0]==0){a=a/fac[i][0];all[i]++;}
while(b%fac[i][0]==0){b=b/fac[i][0];all[i]--;}
}
for(int i=1;i<=num;i++)
{
if(all[i]<fac[i][1])return 0;
}
return 1;
}
int main(void)
{int a,b;
//freopen("123.txt","r",stdin);
//freopen("abc.txt","w",stdout);
while(scanf("%d %d",&a,&b)!=EOF)
{fau=0;
divide(b);
memset(all,0,sizeof(all));
int anall[large],ans=0,i;
if(fau==0)
for(i=1;i<(a-1);i++)
{if(check(a,i)){anall[++ans]=i+1;//if(i!=(a-i))anall[++ans]=a-i+1;
}
}
//sort(anall+1,anall+1+ans);
printf("%d\n",ans);
if(ans==0){printf("\n");continue;}
for(i=1;i<ans;i++)printf("%d ",anall[i]);
printf("%d\n",anall[ans]);
}
return 0;
}
不知wa在何处的代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int fau;
const int large=1e5+10;
int fac[large][2],num;
void divide(int b)
{num=0;
memset(fac,0,sizeof(fac));
for(int i=2;i<=sqrt(b);i++)
{if(b%i==0)
{ fac[++num][0]=i;
while(b%i==0)
{
fac[num][1]++;
b=b/i;
}
}
}
if(b>large)fau=1;
if(b>1){fac[++num][0]=b;fac[num][1]++;}
}
int all[large];
bool check(int a,int b)
{int i;
for( i=1;i<=num;i++)
{ while(a%fac[i][0]==0){a=a/fac[i][0];all[i]++;}
while(b%fac[i][0]==0){b=b/fac[i][0];all[i]--;}
}
for(int i=1;i<=num;i++)
{
if(all[i]<fac[i][1])return 0;
}
return 1;
}
int main(void)
{int a,b;
//freopen("123.txt","r",stdin);
//freopen("abc.txt","w",stdout);
while(scanf("%d %d",&a,&b)!=EOF)
{fau=0;
divide(b);
memset(all,0,sizeof(all));
a=a-1;
int anall[large],ans=0,i;
if(fau==0)
for(i=1;i<=((a+1)/2);i++)
{if(check(a-i+1,i)){anall[++ans]=i+1;if(i!=(a-i))anall[++ans]=a-i+1;}
}
sort(anall+1,anall+1+ans);
printf("%d\n",ans);
if(ans==0){printf("\n");continue;}
for(i=1;i<ans;i++)printf("%d ",anall[i]);
printf("%d\n",anall[ans]);
}
return 0;
}