NOIP复赛复习(三)文件读写与数论模板
来源:互联网 发布:java中的private 编辑:程序博客网 时间:2024/05/22 14:22
文件读入读出
假设题目名为“add”,那么文件夹名为“add”,c++程序名为“add.cpp”,读入文件名为“add.in”,输出文件名为“add.out”。四个的拼写均不可有误,包括大小写差异。千万不要调试后就忘记修改文件读入读出了。
#include<cstdio>
int main(){
freopen("add.in","r",stdin);//read
freopen("add.out","w",stdout);//write
}
数论算法
1、线性筛
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn=200005;
int prime[maxn];
bool not_prime[maxn];
int main()
{
int n,cnt=0;
scanf("%d",&n);
memset(not_prime,0,sizeof(not_prime));
not_prime[1]=true;
for(inti=2;i<=n;i++)
{
if(!not_prime[i])
prime[++cnt]=i;
for(intj=1;j<=cnt;j++)
{
if(prime[j]*i>n) break;
not_prime[prime[j]*i]=true;
if(i%prime[j]==0) break;
}
}
for(inti=1;i<=cnt;i++)
printf("%d ",prime[i]);
return 0;
}
2、埃氏筛
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
inline int read()
{
char c;
c=getchar();
for(;c>'9'||c<'0';c=getchar());
int sum=0;
for(;c<='9'&&c>='0';c=getchar())sum=sum*10+c-48;
return sum;
}
int n,m;
bool f[10010000];
int main()
{
n=read(),m=read();
memset(f,true,sizeof(f));
f[0]=f[1]=false;
for(inti=2;i<=n;i++)
if(f[i])
for(intj=2;i*j<=n;f[i*j]=false,j++);
for(inti=1;i<=m;i++)
{
int x;
x=read();
if(f[x])printf("Yes\n");
elseprintf("No\n");
}
return 0;
}
3、拓展欧几里得算法
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int x,y;
int exgcd(int a,int b)
{
if(!b)
{
x=1;
y=0;
return a;
}
else
{
int t;
intd=exgcd(b,a%b);
t=x;
x=y;
y=t-a/b*x;
return d;
}
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
exgcd(a,b);
// cout<<__gcd(a,b)<<endl;
cout<<x<<" "<<y<<endl;
return 0;
}
4、GCD&LCM
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int gcd(int a,int b)
{
if(!b) returna;
else returngcd(b,a%b);
}
int lcm(int a,int b)
{
returna/gcd(a,b)*b;
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
cout<<gcd(a,b)<<" "<<lcm(a,b)<<endl;
return 0;
}
5、分解质因数
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
long long n;
scanf("%lld",&n);
for(long longi=2;i<=n;i++)
{
while(n!=i)
{
if(n%i==0)
{
printf("%lld*",i);
n=n/i;
}
elsebreak;
}
}
printf("%lld",n);
return 0;
}
6、大数翻倍法
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int a[233],mo[233];
int gcd(int a,int b)
{
if(!b) returna;
else returngcd(b,a%b);
}
int lcm(int a,int b)
{
returna/gcd(a,b)*b;
}
int main()
{
int n;
scanf("%d",&n);
for(inti=1;i<=n;i++)
scanf("%d%d",&a[i],&mo[i]);
intans=0,nowmo=1;
for(inti=1;i<=n;i++)
{
intother=a[i],othermo=mo[i];
if(othermo>nowmo)
{
swap(ans,other);
swap(nowmo,othermo);
}
while(ans%othermo!=other)
ans+=nowmo;
nowmo=lcm(nowmo,othermo);
}
printf("%d",ans);
}
7、快速幂
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1007;
int xx(int a,int n,int MOD)
{
int ret=1;
int tmp=a%MOD;
while(n)
{
if(n&1)
ret=ret*tmp%MOD;
tmp=tmp*tmp%MOD;
n>>=1;
}
return ret;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)==2)
{
printf("%d\n",xx(m,n,MOD));
}
}
8、位运算
功能
示例
位运算
去掉最后一位
(101101->10110)
x >> 1
在最后加一个0
(101101->1011010)
x << 1
在最后加一个1
(101101->1011011)
x << 1+1
把最后一位变成1
(101100->101101)
x | 1
把最后一位变成0
(101101->101100)
x | 1-1
最后一位取反
(101101->101100)
x ^ 1
把右数第k位变成1
(101001->101101,k=3)
x | (1 << (k-1))
把右数第k位变成0
(101101->101001,k=3)
x & !(1 << (k-1))
右数第k位取反
(101001->101101,k=3)
x ^ (1 << (k-1))
取末三位
(1101101->101)
x & 7
取末k位
(1101101->1101,k=5)
x & (1<< k -1)
取右数第k位
(1101101->1,k=4)
x >> (k-1) & 1
把末k位变成1
(101001->101111,k=4)
x | (1 << k-1)
末k位取反
(101001->100110,k=4)
x ^ (1 << k-1)
把右边连续的1变成0
(100101111->100100000)
x & (x+1)
把右起第一个0变成1
(100101111->100111111)
x | (x+1)
把右边连续的0变成1
(11011000->11011111)
x | (x-1)
取右边连续的1
(100101111->1111)
(x ^ (x+1)) >> 1
去掉右起第一个1的左边
(100101000->1000)
x & (x ^ (x-1))
- NOIP复赛复习(三)文件读写与数论模板
- NOIP复赛复习(三)文件读写与数论模板
- NOIP复赛复习(四)读写外挂与高精度模板
- NOIP复赛复习(四)读写外挂与高精度模板
- NOIP复赛复习(十二)数论算法巩固与提高
- NOIP复赛复习(十二)数论算法巩固与提高
- NOIP复赛复习(七)STL容器与字符串模板
- NOIP复赛复习(六)算法分析与排序模板
- NOIP复赛复习(六)算法分析与排序模板
- NOIP复赛复习(七)STL容器与字符串模板
- NOIP复赛复习(八)STL算法与树结构模板
- NOIP复赛复习(五)程序对拍与图论模板
- NOIP复赛复习(五)程序对拍与图论模板
- NOIP复赛复习(八)STL算法与树结构模板
- NOIP复赛知识点复习:算法/数据结构模板
- NOIP复赛复习(二)竞赛环境与注意事项
- NOIP复赛复习(一)常见问题与常用策略
- NOIP复赛复习(十五)动态规划巩固与提高
- 第六章第二节jsp:useBean创建javabean
- jsp自定义标签封装(tld)
- Tensorflow实例:实现进阶的卷积神经网络
- yii2 干货 学习资料
- C++中类和对象以及成员函数
- NOIP复赛复习(三)文件读写与数论模板
- java快速排序
- java Method 访问方法
- 字符类型问题C2440,C2664
- 输入当前日期,获取下一天的日期
- Unity 一些实用的代码
- homogeneous coordinates 齐次坐标
- js正则查询匹配语句里面的【凋谢】【鼓掌】等 替换成图片
- 多态