题目链接:Curling 2.0
参考博文:POJ 3009 Curling 2.0(DFS + 模拟)
代码:
#include
#include
#include
using namespace std;
typedef long long LL;
const int MAX = 25;
int maze[MAX][MAX];
int dx[5] = {0, 1, 0, -1};
int dy[5] = {1, 0, -1, 0};
int ans;
int w, h;
int sx, sy;
//判断坐标的状态
int check(int x, int y)
{
if(maze[x][y]==0 || maze[x][y]==2) return 1;//可以移动
else if(maze[x][y]==-1 || maze[x][y]==1) return 2;//不能移动
else return 3;//到达目的地
}
void dfs(int x, int y, int t)
{
if(maze[x][y]==3 || t>10)//步数超出规定以及到达目的地均结束
{
if(ans>t) ans = t;
}
else
{
//四个方向遍历
for(int i=0; i<4; i++)
{
int tx = x, ty = y;
if(check(tx+dx[i], ty+dy[i]) != 2)//只进行可以移动的状态操作
{
while(check(tx+dx[i], ty+dy[i])==1)//如果前方可以移动,一直向同一个方向移动,步数不更新
tx += dx[i], ty += dy[i];
if(maze[tx+dx[i]][ty+dy[i]]==1)//前方障碍物
{
//当前方是障碍物时,撞碎变成0,更新了地图
maze[tx+dx[i]][ty+dy[i]] = 0;
t++;
dfs(tx, ty, t);//继续从障碍物前一个开始
t--;
maze[tx+dx[i]][ty+dy[i]] = 1;
}
else if(maze[tx+dx[i]][ty+dy[i]]==3)//到达目的地
{
t++;
dfs(tx+dx[i], ty+dy[i], t);//直接到达目的地,结束
t--;
}
}
}
}
}
int main()
{
while(scanf("%d%d", &w, &h)!=EOF && (w+h))
{
memset(maze, -1, sizeof(maze));
ans = 1<<29;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)
{
scanf("%d", &maze[i][j]);
if(maze[i][j]==2)
sx = i, sy = j;
}
}
dfs(sx, sy, 0);
if(ans>10)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}
题目链接:Meteor Shower
#include
#include
#include
#include
using namespace std;
const int MAX = 500;
struct Node
{
int x, y, t;
Node(int x=-1, int y=-1, int t=-1): x(x), y(y), t(t){}
};
queue q;
int M, X, Y, T;
int vis[MAX][MAX];//-1代表安全区,0代表已经遍历的地区,其余正值代表爆炸的时间
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
void bfs(Node s)
{
while(!q.empty()) q.pop();
q.push(s);
if(vis[s.x][s.y]<0)
{
printf("%d\n", &s.t);
return;
}
if(vis[s.x][s.y]>0 && s.t=0 && y>=0)
{
if(vis[x][y]>0 && tT)
vis[X][Y] = T;
for(int j=0; j<4; j++)
{
int x = X+dx[j], y = Y+dy[j];
if(x>=0 && y>=0)
{
if(vis[x][y]==-1)
vis[x][y] = T;
else if(vis[x][y]>T)
{
vis[x][y] = T;
}
}
}
}
bfs(Node(0, 0, 0));
return 0;
}
题目链接:Smallest Difference
代码:
#include
#include
#include
#include
using namespace std;
int main()
{
int T;
string s;
int a[15];
int b, c;//b是大的那一方
cin >> T;
getchar();
while(T--)
{
getline(cin, s);
int k = 0, cnt;
for(int i=0; i2)) continue;//舍去这个排列
for(int i=0; i
题目链接:Hopscotch
#include
#include
#include
#include
using namespace std;
const int MAX = 10;
struct Node
{
int x, y;
Node(int x=-1, int y=-1): x(x), y(y){}
};
int G[MAX][MAX];
int r[100000][MAX], n;
set a;
Node b[MAX];
//右、左、下、上
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
int cnt = 0;
void dfs(Node s, int step, int num)
{
if(step==5)
{
a.insert(num);
return;
}
for(int i=0; i<4; i++)
{
int x = dx[i]+s.x, y = dy[i]+s.y, t = step+1;
if(x>=0 && x<5 && y>=0 && y<5)
{
dfs(Node(x, y), t, num*10+G[x][y]);
}
}
return;
}
int main()
{
n = 0;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
scanf("%d", &G[i][j]);
}
}
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
dfs(Node(i, j), 0, G[i][j]);
}
}
printf("%d\n", a.size());
return 0;
}
题目链接:Cleaning Shifts
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 25005;
struct Node
{
int s, e;
bool operator < (const Node &A) const
{
return scnt)//起始点可以接上,终点取满足起始点条件的最大值
{
cnt = c[i].e;
}else if(c[i].s>end+1 && c[i].s<=cnt+1 && c[i].e>cnt)//有新的更新目标(即起始点接不上,但满足更新条件)
{
end = cnt;
ans++;
i--;//注意这种情况需要重来一次
}
if(cnt==T)//满足终止条件
{
flag = 1;
break;
}
}
if(flag)
printf("%d\n", ans);
else
printf("-1\n");
return 0;
}
题目链接:Radar Installation
参考博文:Radar Installation
代码:
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 1005;
struct Node
{
double s, e;
bool operator < (const Node &A) const
{
return send)//没有交集
{
ans++;
//start = a[i].s;
end = a[i].e;
}else if(a[i].e<=end)//交集缩小
{
//start = a[i].s;
end = a[i].e;
}
}
printf("%d\n", ans);
}
return 0;
}
题目链接:Stall Reservations
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 50005;
int N, s, t, ans[MAX];
struct Node
{
int s, e, id;//起始点,终止点,编号(用来当下标)
Node(int s = -1, int e = -1, int id = -1): s(s), e(e), id(id){}
};
bool cmp1(Node A, Node B)
{
return A.sB.e;
}
};
Node cows[MAX];
Node stall[MAX];
priority_queue, cmp2> q;//用来筛选stall(选择终止点最小的)
int main()
{
while(scanf("%d", &N)!=EOF)
{
for(int i=0; i
题目链接:Yogurt factory
代码:
#include
#include
#include
using namespace std;
typedef long long LL;
const int MAX = 10005;
int C[MAX], Y[MAX];
int N, S;
LL cost;
int main()
{
scanf("%d%d", &N, &S);
for(int i=0; i
题目链接:Packets
参考博文:POJ1017 Packets(贪心算法训练)
代码:
#include
#include
using namespace std;
int a1, a2, a3, a4, a5, a6, s3, s2;
int main()
{
while(scanf("%d%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5, &a6)!=EOF)
{
if(a1+a2+a3+a4+a5+a6==0) break;
int p = 0;
p += a6/1;
p += a5/1;
a1 = max(a1-a5*11, 0);
p += a4/1;
if(a2<5*a4) a1 = max(a1-(5*a4-a2)*4, 0);
a2 = max(a2-5*a4, 0);
p += a3/4;
s3 = a3%4;
if(s3) p++;
if(s3==1)
{
if(a2<5) a1 = max(a1-(5-a2)*4-7, 0);
else a1 = max(a1-7, 0);
a2 = max(a2-5, 0);
}
else if(s3==2)
{
if(a2<3) a1 = max(a1-(3-a2)*4-6, 0);
else a1 = max(a1-6, 0);
a2 = max(a2-3, 0);
}
else if(s3==3)
{
if(a2<1) a1 = max(a1-(1-a2)*4-5, 0);
else a1 = max(a1-5, 0);
a2 = max(a2-1, 0);
}
p += a2/9;
s2 = a2%9;
if(s2!=0) p++;
if(s2) a1 = max(a1-(36-4*s2), 0);
p += (a1+35)/36;
printf("%d\n", p);
}
return 0;
}
题目链接:Allowance
参考博文:Allowance(POJ-3040)
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 1005;
const int INF = 1<<29;
struct Node
{
int num, value;
bool operator < (const Node &A) const
{
return value < A.value;
}
};
int use[MAX];//记录硬币一周使用的个数
int n, c, cnt;
int main()
{
cnt = 0;
scanf("%d%d", &n, &c);
for(int i=1; i<=n; i++)
scanf("%d%d", &coin[i].value, &coin[i].num);
sort(coin+1, coin+1+n);
//找出硬币币值大于c元的数目
for(int i=1; i=c)
{
cnt += coin[i].num;//直接支付
coin[i].num = 0;//清零
}
}
while(true)
{
bool flag = false;
int sum = c;
memset(use, 0, sizeof(use));
//从大到小逼近c
for(int i=n; i>0; i--)
{
if(coin[i].num>0)
{
use[i] = min(sum/coin[i].value, coin[i].num);
sum -= use[i]*coin[i].value;
if(sum==0)
{
flag = true;
break;
}
}
}
//从小到大抵达或略微超过c
if(sum>0)
{
for(int i=1; i<=n; i++)
{
if(coin[i].num-use[i]>0)
{
while(use[i]0)
minn = min(minn, coin[i].num/use[i]);//记录所有硬币中最小的使用周数
}
for(int i=1; i<=n; i++)
{
if(use[i]>0)//按照求出的使用周数,更新硬币的数目
coin[i].num -= minn*use[i];
}
cnt += minn;
}
printf("%d\n", cnt);
return 0;
}
题目链接:Stripies
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 200;
bool cmp(int a, int b)
{
return a>b;
}
int main()
{
int n, m[MAX];
cin >> n;
for(int i=0; i> m[i];
sort(m, m+n, cmp);//从大到小排序
double b = m[0];
for(int i=1; i
题目链接:Protecting the Flowers
代码:
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int MAX = 100005;
struct Node
{
int t, d;
};
bool cmp(Node a, Node b)
{
double ap = a.d/(double)a.t;
double bp = b.d/(double)b.t;
return ap>bp;
}
Node C[MAX];
LL ans = 0, s = 0;
int main()
{
int N, T, D;
while(scanf("%d", &N)!=EOF)
{
for(int i=0; i
题目链接:Sumsets
参考博文:动态规划之划分数
DP:Sumsets(POJ 2229)
代码:
#include
#include
#include
using namespace std;
const int MAX = 1000001;
int dp[MAX];
int c;
int main()
{
int N;
while(cin >> N)
{
dp[0] = 1;
c = 1;
for(int i=1; i<=20 && c<=N; i++)
{
for(int j=c; j<=N; j++)
{
dp[j] = (dp[j]+dp[j-c])%1000000000;
}
c = c*2;//累乘,节省了时间
}
cout << dp[N] << endl;
}
return 0;
}
题目链接:Milking Time
参考博文:poj 3616 Milking Time —DP(带权重的区间动态规划)
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 1000001;
struct Node
{
int s, e, v;
bool operator < (const Node &A) const
{
return e=b.e+R || b.s>=a.e+R) return 1;
else return 0;
}
void Calculate_P(int M, int R)
{
p[1] = 0;
for(int i=M; i>1; i--)
{
int k=i-1;
while(k>0 && !check(C[k], C[i]))
k--;
p[i] = k;
}
}
int main()
{
while(scanf("%d%d%d", &N, &M, &R)!=EOF)
{
for(int i=1; i<=M; i++)
{
scanf("%d%d%d", &C[i].s, &C[i].e, &C[i].v);
}
sort(C+1, C+M+1);//按结束点排序
Calculate_P(M, R);//计算每一个区间前面可以接着的最近区间下标
memset(dp, 0, sizeof(dp));
for(int i=1; i<=M; i++)
{
dp[i] = max(dp[i-1], dp[p[i]]+C[i].v);//第i个区间是否使用
}
printf("%d\n", dp[M]);
}
return 0;
}
题目链接:Sunscreen
参考博文:Sunscreen (poj 3614 贪心+优先队列)
代码:
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 2505;
struct Sun
{
int spf, num;
};
struct Cow
{
int min, max;
};
struct cmp
{
bool operator ()(Cow a, Cow b)
{
return a.max>b.max;
}
};
bool cmp1(Cow a, Cow b)
{
return a.min, cmp> Q;
Cow Cow[MAX], p;
Sun S[MAX];
int main()
{
int C, L;
cin >> C >> L;
for(int i=0; i> Cow[i].min >> Cow[i].max;
for(int i=0; i> S[i].spf >> S[i].num;
sort(Cow, Cow+C, cmp1);
Cow[C].min = 1<<29, Cow[C].max = 1<<29;//哨点
sort(S, S+L, cmp2);
int k = 0, ans = 0;
for(int i=0; i<=C; i++)
{
if(k==L) break;
if(Cow[i].min<=S[k].spf)
Q.push(Cow[i]);
else if(!Q.empty())//符合该防晒霜的牛已经全部进队列
{
p = Q.top();
Q.pop();
while(p.max=S[k].spf)
{
S[k].num--;
if(S[k].num==0)
k++;
ans++;
}
i--;
}
else if(Q.empty())//该防晒霜不能用,换下一个
{
k++, i--;
}
}
cout << ans << endl;
return 0;
}
题目链接:Moo University - Financial Aid
参考博文:POJ 2010 Moo University – Financial Aid 题解 《挑战程序设计竞赛》
代码:
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 100005;
struct Cow
{
int s, m;
};
bool cmp(Cow a, Cow b)
{
return a.s>b.s;
}
struct cmp1
{
bool operator ()(Cow a, Cow b)
{
return a.m, cmp1>q, p;//取最大的学费
int main()
{
while(scanf("%d%d%d", &N, &C, &F)!=EOF)
{
for(int i=0; i=n)
{
lower[i] = total;
}else
{
lower[i] = 0;
}
q.push(c[i]);
total += c[i].m;
if(q.size()>n)
{
total -= q.top().m;
q.pop();
}
}
//求upper的值
total = 0;
for(int i=C-1; i>=0; i--)
{
if(p.size()>=n)
{
upper[i] = total;
}else
{
upper[i] = 0;
}
p.push(c[i]);
total += c[i].m;
if(p.size()>n)
{
total -= p.top().m;
p.pop();
}
}
//筛选出最终结果
int ans = -1;
for(int i=C; i>=0; i--)
{
if(lower[i]+c[i].m+upper[i]<=F)//判断条件
{
ans = c[i].s;
break;
}
}
if(ans==-1)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}
题目链接:Wireless Network
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 1005;
struct Node
{
int x, y;
};
Node C[MAX];
int a[MAX], d, N, id, id2;
int pre[MAX], Rank[MAX];
char order;
bool check(int x1, int y1, int x2, int y2)
{
if(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=(double)d)
{
return true;
}
return false;
}
void Init()
{
for(int i=0; iRank[fy])
{
pre[fy] = fx;
}else
{
pre[fx] = fy;
if(Rank[fx]==Rank[fy]) Rank[fy]++;
}
}
bool same(int x, int y)
{
return findRoot(x)==findRoot(y);
}
int main()
{
scanf("%d%d", &N, &d);
Init();
for(int i=1; i<=N; i++)
{
scanf("%d%d", &C[i].x, &C[i].y);
}
int k = 0;
while(scanf("%c%d", &order, &id)!=EOF)
{
if(order=='O')
{
a[k++] = id;
for(int i=0; i
题目链接:Find them, Catch them
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 100005;
int pre[MAX*2], Rank[MAX*2];
int N = MAX-1;
//并查集表示关系同时存在
void Init()
{
for(int i=0; i<=2*MAX; i++)
{
pre[i] = i;
Rank[i] = 1;
}
}
int findRoot(int x)
{
int r = x;
while(r!=pre[r])
{
r = pre[r];
}
int j=x, i;
while(j!=r)
{
i = pre[j];
pre[j] = r;
j = i;
}
return r;
}
void join(int x, int y)
{
int fx = findRoot(x), fy = findRoot(y);
if(Rank[fx]>Rank[fy])
{
pre[fy] = fx;
}else
{
pre[fx] = fy;
if(Rank[fx]==Rank[fy]) Rank[fy]++;
}
}
bool same(int x, int y)
{
return findRoot(x)==findRoot(y);
}
int main()
{
int T, N, M, a, b;
char order;
scanf("%d", &T);
while(T--)
{
Init();
scanf("%d%d", &N, &M);
for(int i=0; i
题目链接:Six Degrees of Cowvin Bacon
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 500;
const int INF = 1<<29;
int G[MAX][MAX], a[MAX];
int cnt = 0, Min = INF, ans;
int N, M, n;
int main()
{
scanf("%d%d", &N, &M);
for(int i=0; i<=N; i++)
{
for(int j=0; j<=N; j++)
G[i][j] = INF;
}
while(M--)
{
scanf("%d", &n);
for(int i=0; i=0; j--)
{
G[a[j]][a[i]] = 1;
G[a[i]][a[j]] = 1;
}
}
}
for(int k=1; k<=N; k++)
{
for(int i=1; i<=N; i++)
{
if(G[i][k]==INF) continue;
for(int j=1; j<=N; j++)
{
if(G[k][j]==INF) continue;
G[i][j] = min(G[i][j], G[i][k]+G[k][j]);
}
}
}
Min = INF;
double r;
for(int i=1; i<=N; i++)
{
cnt = 0, ans = 0;
for(int j=1; j<=N; j++)
{
if(i==j) continue;
if(G[i][j]!=INF)
{
ans++;
cnt += G[i][j];
}
}
Min = min(Min, cnt);
//cout << cnt << endl;
r = Min/(1.0*ans)*100;
}
Min = r;
printf("%d\n", Min);
return 0;
}
题目链接:Wormholes
代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 505;
const int INF = 1<<29;
int G[MAX][MAX], d[MAX];
int F, N, M, W, En, S, E, T;
struct Edge
{
int from, to, w;
}e[5005];
void Bellman_Ford(int s)
{
fill(d, d+MAX, INF);
d[s] = 0;
int k = 0;
while(1)
{
int flag = 0;
for(int i=0; id[e[i].from]+e[i].w)
{
d[e[i].to] = d[e[i].from]+e[i].w;
flag = 1;
}
}
if(!flag) break;
k++;
if(k>=N) break;
}
if(k>=N)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
scanf("%d", &F);
while(F--)
{
En = 0;
scanf("%d%d%d", &N, &M, &W);
for(int i=0; i
题目链接:Silver Cow Party
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 1005;
const int INF = 1<<29;
int G[MAX][MAX];
int d[MAX], d2[MAX], vis[MAX];
int N, M, X, A, B, T;
void dijstra(int s, int (&d)[MAX])
{
fill(d, d+N+2, INF);
memset(vis, 0, sizeof(vis));
d[s] = 0;
while(1)
{
int minv = INF, u;
for(int i=1; i<=N; i++)
{
if(vis[i]==0 && d[i]
题目链接:Bad Cowtractors
注意:使用邻接表可以方便处理结点之间有重边的情况,并且效率还高,推荐使用邻接表实现。
代码:
#include
#include
#include
#include
#include
using namespace std;
/*
使用了邻接表,不需要考虑重边,因为所以的边均保存了;
如果是邻接矩阵,需要保存最大或最小的边
*/
const int MAX = 1005;
const int INF = 1<<29;
int N, M;
int A, B, C;
int vis[MAX], d[MAX];
struct Node
{
int to, w;
Node(int to=-1, int w=-1): to(to), w(w){}
};
vectorG[MAX];
void Prim()
{
fill(d, d+MAX, INF);
memset(vis, 0, sizeof(vis));
d[1] = 0;
while(1)
{
int Minv = INF, u;
for(int i=1; i<=N; i++)
{
if(vis[i]==0 && d[i]v.w)
{
d[v.to] = v.w;
}
}
}
int sum = 0, flag = 0;
//cout << endl;
for(int i=1; i<=N; i++)
{
if(d[i]!=INF)
sum += d[i];
else
flag = 1;
}
if(!flag)
printf("%d\n", -sum);
else
printf("-1\n");
}
int main()
{
cin >> N >> M;
for(int i=0; i> A >> B >> C;
G[A].push_back(Node(B, -C));
G[B].push_back(Node(A, -C));
}
Prim();
return 0;
}
题目链接:Out of Hay
代码:
#include
#include
#include
#include
#include
using namespace std;
/*
使用了邻接表,不需要考虑重边,因为所以的边均保存了;
如果是邻接矩阵,需要保存最大或最小的边
*/
const int MAX = 2005;
const int INF = 1<<29;
int N, M;
int A, B, C;
int vis[MAX], d[MAX];
struct Node
{
int to, w;
Node(int to=-1, int w=-1): to(to), w(w){}
};
vectorG[MAX];
void Prim()
{
fill(d, d+MAX, INF);
memset(vis, 0, sizeof(vis));
d[1] = 0;
while(1)
{
int Minv = INF, u;
for(int i=1; i<=N; i++)
{
if(vis[i]==0 && d[i]v.w)
{
d[v.to] = v.w;
}
}
}
int Max = 0;
//cout << endl;
for(int i=1; i<=N; i++)
{
Max = max(Max, d[i]);
}
printf("%d\n", Max);
}
int main()
{
cin >> N >> M;
for(int i=0; i> A >> B >> C;
G[A].push_back(Node(B, C));
G[B].push_back(Node(A, C));
}
Prim();
return 0;
}
题目链接:Dead Fraction
参考博文:poj 1930 Dead Fraction
参考博文:POJ - 1930 Dead Fraction(简单数学推理)
题目链接:Prime Path
代码:
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 10000;
int isPrime[MAX];
int vis[MAX];
int T, from, to;
struct Node
{
int n, t;//n是数字,t是步数
Node(int n=-1, int t=-1): n(n), t(t){}
};
queue q;
void getPrime()
{
fill(isPrime, isPrime+MAX, 1);
isPrime[1] = 0;
for(int i=2; 2*i
题目链接:X-factor Chains
参考博文:POJ 3421 X-factor Chains (因式分解+排列组合)
代码:
#include
#include
#include
using namespace std;
const int MAX = 1<<20+1;
typedef long long LL;
LL X, p[MAX], a[MAX];
int main()
{
while(scanf("%lld", &X)!=EOF)
{
int cnt = 0;
//素因子分解(因为从小到大分解,得到的一定是素数)
for(LL i=2; i*i<=X; i++)
{
if(X%i==0)
{
p[cnt] = i;
a[cnt++] = 1;
X /= i;
}
while(X%i==0)
{
a[cnt-1]++;
X /= i;
}
}
if(X>1)//这一步容易遗忘
{
p[cnt] = X;
a[cnt++] = 1;
}
//相关计算
LL A = 0, ans = 1;
for(int i=0; i
题目链接:Semi-prime H-numbers
参考博文:Semi-prime H-numbers POJ - 3292(简单打表)
先看别人的代码:
#include
#include
#include
#include
#include
#include
#include
自己的代码:
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int MAX = 1000002;
int h, flag[MAX], ans[MAX];
vector v;
int r[MAX];
//打表+筛选
void isHPrime()
{
memset(flag, 1, sizeof(flag));
//得到H素数,存入数组,并将H-composite标记为0
for(int i=1; i<=MAX/4; i++)
{
int j = 4*i+1;
if(j>=MAX) break;
if(flag[j])
{
for(int k=2; k=MAX) break;
flag[4*a+1] = 0;
}
v.push_back(j);
}
}
//标记所有的H_s_p
int k = 0;
r[k++] = 0;//作为一个初始数据方便下面的程序
for(int i=0; i
题目链接:Raising Modulo Numbers
代码:
#include
#include
#include
using namespace std;
typedef long long LL;
LL Z, H, M, A, B;
LL Pow(LL a, LL p)
{
LL ans = 1, t = p;
while(t)
{
if(t&1) ans = (a*ans)%M;
a = (a*a)%M;
t >>= 1;
}
return ans;
}
int main()
{
scanf("%lld", &Z);
while(Z--)
{
scanf("%lld%lld", &M, &H);
LL cnt = 0;
for(LL i=0; i
题目链接:Pseudoprime numbers
代码:
#include
#include
#include
using namespace std;
typedef long long LL;
LL p, a;
int IsPrime(LL p)
{
for(LL i=2; i*i<=p; i++)
{
if(p%i==0)
return 0;
}
return 1;
}
LL Pow(LL a, LL p)
{
LL ans = 1, t = p;
while(t)
{
if(t&1) ans = (a*ans)%p;
a = (a*a)%p;
t >>= 1;
}
return ans;
}
void solve(LL a, LL p)
{
if(IsPrime(p)||Pow(a, p)!=a%p)
printf("no\n");
else
printf("yes\n");
}
int main()
{
while(scanf("%lld%lld", &p, &a)!=EOF && (a+p))
{
solve(a, p);
}
return 0;
}