zoukankan      html  css  js  c++  java
  • bzoj1406[AHOI2007]密码箱

    bzoj1406[AHOI2007]密码箱

    题意:

    输出1到n-1中平方后模n等于1的整数

    题解:

    设所求数x,化简得(x+1)(x-1)=n*k,设n1*n2等于k,(x+1)%n1==0,(x-1)%n2==0,因此n1、n2都为n的因数,且一个≤sqrt(n),一个≥(sqrt(n))。据说int以内的数的因数都不超过30个,所以可以先求出所有≥sqrt(n)的因数,然后对于每个因数,求出所有<n的这个因数的倍数,用它尝试被x+1模。因为这些因数都≥sqrt(n),所以这个枚举的过程不会超时。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define maxn 100
     7 using namespace std;
     8 
     9 int yss,ys[maxn],st[maxn*5000],sts;
    10 int main(){
    11     int n; scanf("%d",&n); inc(i,1,(int)sqrt(n))if(n%i==0)ys[++yss]=n/i; st[sts=1]=1;
    12     inc(i,1,yss){
    13         for(int j=ys[i];j<=n;j+=ys[i]){
    14             if((j-2)%(n/ys[i])==0)st[++sts]=j-1;
    15             if(j<n&&(j+2)%(n/ys[i])==0)st[++sts]=j+1;
    16         }
    17     }
    18     if(sts==0||n==1)printf("None");else{
    19         sort(st+1,st+1+sts); int m=unique(st+1,st+1+sts)-st-1; inc(i,1,m)printf("%d
    ",st[i]);
    20     }
    21     return 0;
    22 }

    20160603

  • 相关阅读:
    省选测试28
    省选测试27
    省选测试26
    省选测试25
    省选测试24
    省选测试23
    省选测试22
    省选测试21
    关于maven 导入依赖的最终处理问题
    Dubbo 2 之抽取公共接口
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5689545.html
Copyright © 2011-2022 走看看