您的位置:首页 > 其它

九度 1441 1442 1443二分求幂法

2018-01-30 23:01 141 查看


【九度OJ】题目1441:人见人爱 A ^ B 解题报告

标签(空格分隔): 九度OJ

原题地址:http://ac.jobdu.com/problem.php?pid=1441


题目描述:

求A^B的最后三位数表示的整数。说明:A^B的含义是“A的B次方”


输入:

输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。


输出:

对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。


样例输入:

2 3
12 6
6789 10000
0 0


1

2

3

4

5


样例输出:

8
984
1

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==0&&b==0)break;
int ans=1;
while(b!=0)
{
if(b%2==1)
{
ans*=a;
ans%=1000;
}
b/=2;
a*=a;
a%=1000;
}
printf("%d\n",ans);
}
return 0;
}



九度OJ 1442 A sequence of numbers

题目描述:

Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear
now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help.

输入:
The first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain four integers. The first
three indicating the first three numbers of the sequence, and the last one is K, indicating that we want to know the K-th numbers of the sequence.
You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63). All the numbers of the sequences are integers. And
the sequences are non-decreasing.

输出:
Output one line for each test case, that is, the K-th number module (%) 200907.

样例输入:
2
1 2 3 5
1 2 4 5


样例输出:
5
16


#include <iostream>

#include<stdio.h>

#include<string.h>

#define N 200907

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
long long a,b,c,k;
long long ans=1; 
scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
if(b-a==c-b)
{
ans=a+(k-1)/(b-a);
}
else if(b/a==c/b)
{
long long q=b/a;
long long t=k-1;
while(t!=0)
{
if(t%2==1)
{
ans*=q;
ans%=N;
}
q*=q;
t/=2;
q%=N;
}
}
printf("%d\n",ans);
}
}
return 0;

}
题目1443:Tr A


题目描述:
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。

输入:
数据的第一行是一个T,表示有T组数据。每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。

输出:
对应每组数据,输出Tr(A^k)%9973。

样例输入:
2
2 2
1 0
0 1
3 99999999
1 2 3
4 5 6
7 8 9


样例输出:
2
2686


#include <iostream>

#include<stdio.h>

#include<string.h>

#define max 9973

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int n,k;

int t;

int buf[15][15];

int tmp1[15][15];

int tmp2[15][15];

int times(int a[][15],int b[][15],int n)

{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tmp2[i][j]=0;
for(int k=0;k<n;k++)
{
tmp2[i][j]+=a[i][k]*b[k][j];
tmp2[i][j]%=max;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=tmp2[i][j];
}
}

}

int main(int argc, char** argv) {
while(scanf("%d",&t)!=EOF)
{
for(int c=0;c<t;c++)
{
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&buf[i][j]);
tmp1[i][j]=buf[i][j];
}
}
k--;
while(k>0)
{
if(k%2==1)
{
times(buf,tmp1,n);
k--;
}
else
{
times(tmp1,tmp1,n);
k/=2;
}
}
int ans=0;
for(int i=0;i<n;i++)
{
ans+=buf[i][i];
ans%=max;
}
printf("%d\n",ans);
}
}
return 0;

}
上面这是成功了的,也有失败的,如下。我实在找不出bug在哪里。

#include<stdio.h>
#include<string.h>
#define M 9973
int buf[15][15];
int tmp1[15][15];
int tmp2[15][15];
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
for(int c=0;c<t;c++){
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&buf[i][j]);
tmp1[i][j]=buf[i][j];
tmp2[i][j]=buf[i][j];
}
}
int tmp;
int s=k-1;
while(s>0)
{
if(s%2==1)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tmp=0;
for(int k=0;k<n;k++)
{
tmp+=buf[i][k]*tmp2[k][j];
tmp%=M;
}
tmp1[i][j]=tmp;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
buf[i][j]=tmp1[i][j];
}
}
s--;
}
else
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tmp=0;
for(int k=0;k<n;k++)
{
tmp+=buf[i][k]*buf[k][j];
tmp%=M;
}
tmp1[i][j]=tmp;
}
}
for(int i=0;i<n;i++)
{	for(int j=0;j<n;j++)
{
buf[i][j]=tmp1[i][j];
}
}
s/=2;
}
}
int ans=0;
for(int i=0;i<n;i++)
{
ans+=buf[i][i];
ans%=M;
}
printf("%d\n",ans);
}
}
 } 

气人,bug在哪里。为什么答案不一样。!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: