0
点赞
收藏
分享

微信扫一扫

algorithm 题集一 (16.04.30)

诗与泡面 2022-09-11 阅读 68


在ACM OJ上刷了一些简单题/基础题,记录下来,心情不好的时候来写吧。

51 nod 1113 矩阵快速幂

简单的矩阵快速幂
​​​http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113​​

#include <stdio.h>
const int mod=1e9+7;
typedef long long LL;
int n,m;
struct matrix{
LL m[105][105];
}A,I;
matrix multi(matrix a,matrix b){
matrix c;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
c.m[i][j]=0;
for(int k=0;k<n;k++){
c.m[i][j]=c.m[i][j]+a.m[i][k]*b.m[k][j]%mod;
}
c.m[i][j]%=mod;
}
}
return c;
}
matrix power(){
matrix ans=I,temp=A;
while(m){
if(m&1) ans=multi(ans,temp);
temp=multi(temp,temp);
m>>=1;
}
return ans;
}
int main()
{
for(int i=0;i<105;i++) I.m[i][i]=1;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) scanf("%lld",&A.m[i][j]);
}
matrix ans=power();
for(int i=0;i<n;i++){
for(int j=0;j<n-1;j++) printf("%lld ",ans.m[i][j]);
printf("%lld\n",ans.m[i][n-1]);
}
}
return 0;
}

少用%可以提高效率。

51nod 1013 3的幂的和

​​http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013​​​
分析:即计算3n+1−12mod (X)

hdu 2845 Beans

​​http://acm.hdu.edu.cn/showproblem.php?pid=2845​​

algorithm 题集一 (16.04.30)_php


分析:dp简单题。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2e5+10;
int x[N],y[N];
int main()
{
//freopen("cin.txt","r",stdin);
int n,m,t;
while(cin>>n>>m){
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
for(int i=2;i<=n+1;i++){
for(int j=2;j<=m+1;j++){
scanf("%d",&t);
x[j]=max(x[j-1],x[j-2]+t);
}
y[i]=max(y[i-1],y[i-2]+x[m+1]);
}
printf("%d\n",y[n+1]);
}
return 0;
}

nefu 1006 莫比乌斯函数

​​http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1006​​​
大意:给出N,求出它的mu[n]。

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
int n;
while(cin>>n){
int ans=1;
for(int i=2;i*i<=n;i++){
if(n%i==0){
int cnt=0;
while(n%i==0){
n/=i;
cnt++;
}
if(cnt>1) {
ans=0;
break;
}
else ans=ans*(-1);
}
}
if(ans!=0&&n>1){
ans=ans*(-1);
}
printf("%d\n",ans);
}
return 0;
}

POj 2183 Bovine Math Geniuses

​​http://poj.org/problem?id=2183​​​
分析: 随机数模拟即可

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1e6;
int time[N];
bool vis[N];
int main()
{
int s;
while(cin>>s){
memset(vis,0,sizeof(vis));
memset(time,0,sizeof(time));
int number=0,len=0,sum=0;
int t=s;
while(1){
t=t/10;
t=t%10000;
t=t*t;
t=t%1000000;
sum++;
if(vis[t]){
len=sum-time[t];
number=t;
break;
}
vis[t]=1;
time[t]=sum;
}
printf("%d %d %d\n",number,len,sum);
}
return 0;
}

POJ 2407 Relatives

​​http://poj.org/problem?id=2407​​​
求解N内和N互质的数。
分析:欧拉函数

codeforces 519A. A and B and Chess

​​http://codeforces.com/problemset/problem/519/A​​​
分析:简单模拟

#include <iostream>
#include <cstdio>
using namespace std;
char g[10][10];
int main()
{
while(~scanf("%s",g[0])){
for(int i=1;i<8;i++){
scanf("%s",g[i]);
}
int white=0;
int black=0;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
switch (g[i][j]){
case 'Q': white+=9; break;
case 'R': white+=5; break;
case 'B': white+=3; break;
case 'N': white+=3; break;
case 'P': white+=1; break;
case 'q': black+=9; break;
case 'r': black+=5; break;
case 'b': black+=3; break;
case 'n': black+=3; break;
case 'p': black+=1; break;
default : break;
}
}
}
if(white>black) puts("White");
else if(white==black) puts("Draw");
else puts("Black");
}
return 0;
}

CF 515 A. Drazil and Date

​​http://codeforces.com/problemset/problem/515/A​​​
大意:从原点走到任意坐标位置用S步能否到达。

codeforces 515 B. Drazil and His Happy Friends

​​http://codeforces.com/problemset/problem/515/B​​​
大意:有n个男孩,m个女孩。其中b个男孩是开心的,g个女孩是开心的。i从0开始遍历,对于第 i mod n个男孩和第i mod n个女孩,如果有一个是开心的,那么两个人都会变得开心,求解能否经过若干天后所有人都会变得开心。

分析:关键是分析i的上限。我是n*m/gcd(n,m) –> n*m –>n*m*n 才过的。听别人说,上界该是2*n*m。即两个n*m

codeforces 515C Drazil and Factorial

​​http://codeforces.com/problemset/problem/515/C​​​
大意:给出一个数字x,F(x)是各个位上数字阶乘的乘积,求解最大的数字a,满足F(a)=F(x),且a上的没有0和1.
分析:对每一个数字分解即可

codeforces 513A. Game

​​http://codeforces.com/problemset/problem/513/A​​​
大意:两个人各有一些球,谁先扔完谁就输。每次可以扔1—ki个

nyist 2 括号配对问题

​​http://acm.nyist.net/JudgeOnline/problem.php?pid=2​​​
现在,有一行括号序列,请你检查这行括号是否配对。
分析:简单题,栈的应用

nyist 76 超级台阶

​​http://acm.nyist.net/JudgeOnline/problem.php?pid=76​​​
有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法?
注:规定从一级到一级有0种走法。
分析:简单dp

nyist 44 子串和

​​http://acm.nyist.net/JudgeOnline/problem.php?pid=44​​​
输出和最大的连续子串的和。
分析:动态规划

#include <iostream>
#include <cstdio>
using namespace std;
const int N=1e6+10;
int sum[N];
int main()
{
int t,n;
cin>>t;
while(t--){
scanf("%d",&n);
sum[0]=0;
int a=0,ans=-101;
for(int i=1;i<=n;i++){
scanf("%d",&a);
if(a<sum[i-1]+a){
sum[i]=sum[i-1]+a;
}
else sum[i]=a;
ans=ans>sum[i]?ans:sum[i];
}
printf("%d\n",ans);
}
return 0;
}

codeforce 514A - Chewbaсca and Number

​​http://codeforces.com/problemset/problem/514/A​​​
大意:把一个数字变小,i位上的数字变成9-i,不能有前导0

CF 519 C. A and B and Team Training

​​http://codeforces.com/problemset/problem/519/C​​​
大意:将老队员和年轻队员组合成若干个队伍,组合方案A:1 old+2 new 组合方案B:2 old+1 new
方案任选,给出老队员和年轻队员的人数,求出可能的最多的队伍?

分析:设采取的两种方案数是A和B,那么
A+2B<=n
2A+B<=m
求解最大的Z=A+B
两个式子处理一下,二重循环变成一重循环。

hdu 1575 Tr A

​​http://acm.hdu.edu.cn/showproblem.php?pid=1575​​​
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
分析:矩阵快速幂

codeforces 7C - Line

​​http://codeforces.com/problemset/problem/7/C​​​
A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist(output:-1)
又是不定方程:
分析:由与拓展欧几里得得到的解x=cx-bt y=cy+at

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(b==0) {
x=1; y=0; d=a;
return ;
}
exgcd(b,a%b,d,x,y);
LL t=x;
x=y;
y=t-a/b*y;
}
int main()
{
LL a,b,c;
while(~scanf("%lld%lld%lld",&a,&b,&c)){
c=-c;
if(a==0){
if(c%b==0) printf("0 %lld\n",c/b);
else puts("-1");
}
else if(b==0){
if(c%a==0) printf("%lld,c/a);
else puts("-1");
}
else {
LL d,x,y;
exgcd(a,b,d,x,y);
if(c%d!=0) {
puts("-1");
continue;
}
a/=d; b/=d; c/=d;
x=x*c; y=y*c;
printf("%lld %lld\n",x,y);
}
}
return 0;
}

nyist 79 拦截导弹

​​http://acm.nyist.net/JudgeOnline/problem.php?pid=79​​​
分析: 能用贪心也能用dp

CF514B B. Gena’s Code

​​http://codeforces.com/problemset/problem/614/B​​​
大意:有n-1个美丽的数和一个不美丽的数(美丽的数 定义:由0、1组成,最多一个1)
分析:字符串模拟

举报

相关推荐

0 条评论