1441
int main(void)
{
int i;
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
{
if (a==0 && b==0)
break;
a = a%1000;
int result = 1;
for(i=0; i<b; i++)
{
result = (result*a)%1000;
}
printf("%d\n", result);
}
return 0;
}
/**************************************************************
Problem: 1441
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
1442
#include <stdio.h>
#define M 200907
long long expK(long long d, int k)
{
if (k == 0)
return 1;
if (k == 1)
return d;
long long res = expK((d*d)%M, k/2);
if (k%2 == 1)
{
res *= d;
res %= M;
}
return res;
}
int main()
{
int i, n, k;
long long d, a[3], res;
scanf("%d", &n);
for (i=0; i<n; i++)
{
scanf("%lld%lld%lld%d", &a[0], &a[1], &a[2], &k);
if (a[0]-a[1] == a[1]-a[2])
{
d = (a[1]-a[0])%M;
res = (a[0]%M + (k-1)*d) % M;
}
else
{
d = (a[1]/a[0])%M;
res = (a[0]%M * expK(d, k-1)) % M;
}
printf("%lld\n", res);
}
return 0;
}
/************************************************************** Problem: 1442 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
1443
int n;
void mult(int a[N][N], int b[N][N], int c[N][N])
{
int i, j, k;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
c[i][j] = 0;
for (k=0; k<n; k++)
c[i][j] += a[i][k]*b[k][j];
c[i][j] %= M;
}
}
}
void cp(int a[N][N], int b[N][N])
{
int i, j;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
b[i][j] = a[i][j];
}
}
}
void expMat(int a[N][N], int k, int b[N][N])
{
if (k == 1)
{
cp(a, b);
return ;
}
int c[N][N];
mult(a, a, c);
expMat(c, k/2, b);
if (k%2 == 1)
{
cp(b, c);
mult(a, c, b);
}
}
int sum(int a[N][N])
{
int i;
int res = 0;
for (i=0; i<n; i++)
res += a[i][i];
return res%M;
}
int main()
{
int i, j, t, m, k;
int a[N][N], b[N][N];
scanf("%d", &t);
for (m=0; m<t; m++)
{
scanf("%d%d", &n, &k);
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
scanf("%d", &a[i][j]);
}
expMat(a, k, b);
printf("%d\n", sum(b));
}
return 0;
}
/**************************************************************
Problem: 1443
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
1444
#include <stdio.h>
#include <string.h>
#define M 10000000
int id[M+1];
int count[M+1];
int n;
void UF(int n)
{
for (int i=1; i<=n; i++)
{
id[i] = i;
count[i] = 1;
}
}
int find(int i)
{
while (id[i] != i)
i = id[i];
return i;
}
void combine(int i, int j)
{
int a = find(i);
int b = find(j);
if (a == b)
return ;
else
{
count[a] += count[b];
count[b] = 0;
id[b] = a;
}
}
int main(void)
{
int m;
int a, b;
int i;
n = M;
while (scanf("%d", &m) != EOF)
{
UF(n);
for (i=0; i<m; i++)
{
scanf("%d%d", &a, &b);
combine(a, b);
}
int max = 0;
for (i=1; i<=n; i++)
max = count[i]>max ? count[i] : max;
printf("%d\n", max);
}
return 0;
}
1445
#include <stdio.h>
#include <string.h>
#define M 1000
int id[M+1];
int count;
int n;
void UF(int n)
{
for (int i=1; i<=n; i++)
{
id[i] = i;
}
count = n;
}
int find(int i)
{
while (id[i] != i)
i = id[i];
return i;
}
void combine(int i, int j)
{
int a = find(i);
int b = find(j);
if (a == b)
return ;
else
{
id[b] = a;
count --;
}
}
int main(void)
{
int t, m;
int a, b;
int i;
scanf("%d", &t);
for (int j=0; j<t; j++)
{
if (j > 0)
getchar();
scanf("%d %d\n", &n, &m);
UF(n);
for (i=0; i<m; i++)
{
scanf("%d%d", &a, &b);
combine(a, b);
}
printf("%d\n", count);
}
return 0;
}
/************************************************************** Problem: 1445 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:916 kb ****************************************************************/
1446(未完成)
1447
#include <stdio.h>
#include <limits.h>
#define N 100
#define INF (INT_MAX/2)
int n;
int v[N];
int d[N];
int p[N][N];
void init()
{
int i, j;
for (i=0; i<n; i++)
{
v[i] = 0;
d[i] = INF;
for (j=0; j<n; j++)
p[i][j] = INF;
}
}
void printDij()
{
for (int i=0; i<n; i++)
printf("%d ", d[i]);
printf("\n");
}
void dij(int k)
{
int i;
v[k] = 1;
d[k] = 0;
while(k != n-1)
{
for (i=0; i<n; i++)
{
if (!v[i] && p[i][k] + d[k] < d[i])
d[i] = p[i][k] + d[k];
}
int md = INF;
for (i=0; i<n; i++)
{
if (!v[i] && d[i] < md)
{
k = i;
md = d[i];
}
}
if (md == INF)
break;
v[k] = 1;
if (k == n-1)
break;
}
}
int main()
{
int i, m, a, b, t;
while(scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0)
break;
init();
for (i=0; i<m; i++)
{
scanf("%d%d%d", &a, &b, &t);
if (t < p[a-1][b-1])
p[a-1][b-1] = p[b-1][a-1] = t;
}
dij(0);
printf("%d\n", d[n-1]);
}
return 0;
}
1448
typedef struct node {
int key;
struct node *next;
} QUEUE;
QUEUE *q;
int qcount;
void initQueue()
{
q = NULL;
qcount = 0;
}
void insert(int key)
{
if (q == NULL)
{
q = (QUEUE *)malloc(sizeof(QUEUE));
q->key = key;
q->next = NULL;
qcount ++;
return ;
}
QUEUE *a, *b, *c;
a = b = q;
while (a != NULL)
{
if (a->key > key)
break;
b = a;
a = a->next;
}
if (a == q)
{
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = q;
q = c;
qcount ++;
return ;
}
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = a;
b->next = c;
qcount ++;
return ;
}
int pop()
{
if (qcount == 0)
return -1;
int key = q->key;
q = q->next;
qcount --;
return key;
}
void printQueue()
{
QUEUE *a = q;
while (a != NULL)
{
printf("%d ", a->key);
a = a->next;
}
printf("\n");
}
int n;
int p[N][N];
int c[N];
int res[N], rcount;
int search(int k)
{
for (int i=0; i<rcount; i++)
{
if (res[i] == k)
return 1;
}
return 0;
}
void print()
{
int i;
for (i=0; i<rcount-1; i++)
printf("%d ", res[i]);
printf("%d\n", res[i]);
}
int topoSort()
{
int i;
for (i=0; i<n; i++)
{
if (c[i] == 0)
insert(i);
}
if (qcount == 0)
return 0;
//printQueue();
rcount = 0;
while (qcount)
{
int k = pop();
res[rcount++] = k;
//printf("qcount=%d, k=%d\n", qcount, k);
for (i=0; i<n; i++)
{
if (p[k][i])
{
c[i]--;
if (c[i] == 0)
insert(i);
}
}
}
if (rcount == n)
return 1;
else
return 0;
}
int main()
{
int i, j, m, x, y;
while(scanf("%d%d", &n, &m) != EOF && n)
{
memset(p, 0, sizeof(p));
memset(c, 0, sizeof(c));
for (i=0; i<m; i++)
{
scanf("%d%d", &x, &y);
p[x][y] = 1;
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
c[j] += p[i][j];
}
}
initQueue();
int islegal = topoSort();
if (islegal)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
/**************************************************************
Problem: 1448
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:1084 kb
****************************************************************/
1449
typedef struct node {
int key;
struct node *next;
} QUEUE;
QUEUE *q;
int qcount;
void initQueue()
{
q = NULL;
qcount = 0;
}
void insert(int key)
{
if (q == NULL)
{
q = (QUEUE *)malloc(sizeof(QUEUE));
q->key = key;
q->next = NULL;
qcount ++;
return ;
}
QUEUE *a, *b, *c;
a = b = q;
while (a != NULL)
{
if (a->key > key)
break;
b = a;
a = a->next;
}
if (a == q)
{
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = q;
q = c;
qcount ++;
return ;
}
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = a;
b->next = c;
qcount ++;
return ;
}
int pop()
{
if (qcount == 0)
return -1;
int key = q->key;
q = q->next;
qcount --;
return key;
}
void printQueue()
{
QUEUE *a = q;
while (a != NULL)
{
printf("%d ", a->key+1);
a = a->next;
}
printf("\n");
}
int n;
int p[N][N];
int c[N];
int res[N], rcount;
void topoSort()
{
int i;
for (i=0; i<n; i++)
{
if (c[i] == 0)
insert(i);
}
//printQueue();
rcount = 0;
while (qcount)
{
int k = pop();
res[rcount++] = k;
//printf("qcount=%d, k=%d\n", qcount, k);
for (i=0; i<n; i++)
{
if (p[k][i])
{
c[i]--;
if (c[i] == 0)
insert(i);
}
}
}
}
void print()
{
int i;
//printf("=====Queue: ");
for (i=0; i<rcount-1; i++)
printf("%d ", res[i]+1);
printf("%d\n", res[i]+1);
}
int main()
{
int i, j, m, x, y;
while(scanf("%d%d", &n, &m) != EOF)
{
memset(p, 0, sizeof(p));
memset(c, 0, sizeof(c));
for (i=0; i<m; i++)
{
scanf("%d%d", &x, &y);
p[x-1][y-1] = 1;
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
c[j] += p[i][j];
}
}
initQueue();
topoSort();
print();
}
return 0;
}
/**************************************************************
Problem: 1449
User: liangrx06
Language: C
Result: Accepted
Time:20 ms
Memory:2024 kb
****************************************************************/
1450
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 2000
typedef struct node {
int key;
struct node *next;
} QUEUE;
QUEUE *q;
int qcount;
void initQueue()
{
q = NULL;
qcount = 0;
}
void insert(int key)
{
if (q == NULL)
{
q = (QUEUE *)malloc(sizeof(QUEUE));
q->key = key;
q->next = NULL;
qcount ++;
return ;
}
QUEUE *a, *b, *c;
a = b = q;
while (a != NULL)
{
if (a->key > key)
break;
b = a;
a = a->next;
}
if (a == q)
{
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = q;
q = c;
qcount ++;
return ;
}
c = (QUEUE *)malloc(sizeof(QUEUE));
c->key = key;
c->next = a;
b->next = c;
qcount ++;
return ;
}
int pop()
{
if (qcount == 0)
return -1;
int key = q->key;
q = q->next;
qcount --;
return key;
}
void printQueue()
{
QUEUE *a = q;
while (a != NULL)
{
printf("%d ", a->key+1);
a = a->next;
}
printf("\n");
}
int n;
int p[N][N];
int c[N];
int res[N], rcount;
char name[N][100];
int topoSort()
{
int i;
int bestCount = 0;
for (i=0; i<n; i++)
{
if (c[i] == 0)
{
bestCount ++;
insert(i);
}
}
return bestCount;
rcount = 0;
while (qcount)
{
int k = pop();
res[rcount++] = k;
for (i=0; i<n; i++)
{
if (p[k][i])
{
c[i]--;
if (c[i] == 0)
insert(i);
}
}
}
}
void print()
{
int i;
for (i=0; i<rcount-1; i++)
printf("%d ", res[i]+1);
printf("%d\n", res[i]+1);
}
int getId(char s[])
{
int i;
for (i=0; i<n; i++)
{
if (strcmp(name[i], s) == 0)
return i;
}
strcpy(name[n++], s);
return n-1;
}
int main()
{
int i, j, m, x, y;
char s1[100], s2[100];
while(scanf("%d", &m) != EOF && m)
{
memset(p, 0, sizeof(p));
memset(c, 0, sizeof(c));
n = 0;
for (i=0; i<m; i++)
{
scanf("%s%s", s1, s2);
x = getId(s1);
y = getId(s2);
p[x][y] = 1;
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
c[j] += p[i][j];
}
}
initQueue();
int res = topoSort();
if (res == 1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}