很简洁的题目。求出x^2%n=1的所有x<=n的值。 n<=2e9.

直接枚举x一定是超时的。 看看能不能化成有性质的式子。

有 (x+1)(x-1)%n==0,设n=a*b,那么一定有x+1=k1a,x-1=k2b. 不妨设a<=b.那么就能O(sqrt(n))枚举a。

然后再枚举x,验证x是否满足这两个式子。注意不能令x=k1a-1.由于a比较小,枚举x=k2b+1,k2b-1即可。

另外set很好用啊。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... set<LL>::iterator it;
set<LL>S; int main ()
{
LL a, b, x, n;
scanf("%lld",&n);
for (int i=; i*i<=n; ++i) {
if (n%i) continue;
a=i; b=n/i;
for (int k=; (x=b*k+)<n; ++k) if ((x+)%a==) S.insert(x);
for (int k=; (x=b*k-)<n; ++k) if ((x-)%a==) S.insert(x);
}
for (it=S.begin(); it!=S.end(); ++it) printf("%lld\n",*it);
return ;
}

BZOJ 1406 密码箱(数论)的更多相关文章

  1. BZOJ 1406 密码箱

    直接两层枚举就行了. 避免排序可以用set. #include<iostream> #include<cstdio> #include<cstring> #incl ...

  2. BZOJ 1406: [AHOI2007]密码箱

    二次联通门 : BZOJ 1406: [AHOI2007]密码箱 /* BZOJ 1406: [AHOI2007]密码箱 数论 要求 x^2 ≡ 1 (mod n) 可以转换为 x ^ 2 - k * ...

  3. BZOJ 1406: [AHOI2007]密码箱( 数论 )

    (x+1)(x-1) mod N = 0, 枚举N的>N^0.5的约数当作x+1或者x-1... ------------------------------------------------ ...

  4. bzoj 1406: [AHOI2007]密码箱 二次剩餘

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 701  Solved: 396[Submit][Status] D ...

  5. bzoj 1406 数论

    首先问题的意思就是在找出n以内的所有x^2%n=1的数,那么我们可以得到(x+1)(x-1)=y*n,那么我们知道n|(x+1)(x-1),我们设n=a*b,那么我们对于任意的a,我们满足n%a==0 ...

  6. 【BZOJ 1406】 [AHOI2007]密码箱

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] \(x^2%n=1\) \(x^2-1 = k*n\) \((x+1)*(x-1) % n == 0\) 设\(n=a*b\) 对于 ...

  7. BZOJ 1406: [AHOI2007]密码箱 exgcd+唯一分解定理

    推出来了一个解法,但是感觉复杂度十分玄学,没想到秒过~ Code: #include <bits/stdc++.h> #define ll long long #define N 5000 ...

  8. BZOJ 2142 礼物 数论

    这道题是求组合数终极版. C(n,m) mod P n>=1e9 m>=1e9 P>=1e9且为合数且piqi<=1e5 拓展lucas定理. 实际上就是一点数论小知识的应用. ...

  9. 【bzoj1406】 AHOI2007密码箱 数论

    在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可发现,这些图标表示一个数 ...

随机推荐

  1. 20145234黄斐《Java程序设计》第九周学习总结

    教材学习内容总结 JDBC Java语言访问数据库的一种规范,是一套API.JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语 ...

  2. stm32中如何进行printf重定向用于串口调试输出

    1 在main中包含stdio.h 文件 2 Target选项框里选Use MicroLib 选项 3 在main中添加UART1_Configuration()初始化的代码 Uart1初始化,voi ...

  3. vue.js中引入其他文件export的方法:

    import {GetPosition} from '../../lib/utils'  //找到 该方法的文件路径,然后 用{}拿到 该方法 var position =GetPosition(); ...

  4. MyBatis-mybatis全局映射文件解析

    全局配置文件为mybatis-config.xml 1.properties标签 <properties resource="dbconfig.properties"> ...

  5. mysql 常用语句使用

    1.查询语句 SELECT * FROM table 2.更改语句 UPDATE table SET name = '123456' WHERE id = 100 3.插入语句 INSERT INTO ...

  6. Bing wallpaper api

    list: http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-cn idx:-1为明天,1为 ...

  7. RF上传图片各种失败坑,使用pywin32来操作windows窗体

    这个上传按钮,使用 Choose File,失败不知道为什么... Name:Choose FileSource:Selenium2Library <test library>Argume ...

  8. Android Test和Logcat

    一 测试相关概念 是否有源码 黑盒测试: 测试工具 白盒测试: 对所有的源码特别熟悉 对特定的代码进行测试 都是编程 时间 单元测试(程序员) 模块测试 集成测试 系统测试 回归测试(改bug) 压力 ...

  9. Java开发工程师(Web方向) - 02.Servlet技术 - 第4章.JSP

    第4章--JSP JSP JSP(Java Server Pages) - 中文名:Java服务器页面 动态网页技术标准 JSP = Html + Java + JSP tags 在服务器端执行,返回 ...

  10. C if语句判断年龄

    #include <stdio.h> int main(int argc, char **argv) { //新建两个变量给变量赋值跟初始化:const int a=45;int c=0; ...