您的位置:首页 > 其它

SicilyOJ 11珠海赛重现 E.Divisibility(数学)

2014-03-30 22:45 141 查看
Description

   We know that a number is divisible by 3 if and only if the sum of its digits is divisible by 3. 

We also know that a number is divisible by 11 if and only if the difference between the sum of the 

odd numbered digits (1st, 3rd, 5th...) and the sum of the even numbered digits (2nd, 4th...) is 

divisible by 11. However, these rules are only for decimal (base-10) numbers. In base-b, 

there are numbers similar to 3 and 11 in decimal. Your task is to find the smallest x and y, 

where x is similar to 3 in base-b and y is similar to 11 in base-b. Formally, you are to find the 

smallest x and y (x, y > 1), so that a base-b number 

n = ak * bk + ak-1 * bk-1 + ... + a1 * b + a0 is divisible by x if and only if ak + ... + a0 is divisible by x, 

and n is divisible by y if and only if (a0 + a2 + ...) - (a1 + a3 + ...) is divisible by y.

Input

  The first line is a single integer T (1 <= T <= 100), indicating the number of test cases.
  For each test case, there is only one positive integer b (3 <= b <= 10^6), the base. 
 

Output

For each test case, output the x and y in a line, separated by a single space. The x and y 
should be presented in decimal.
 

Sample Input
 Copy sample input to clipboard

2
10
120


Sample Output

3 11
7 11



题意:对于一个十进制数,对于除数3,一个数能被三整除,那么它的所有数位相加的和为3的倍数;对于除数11,一个数能被11整除,
那么它的奇数位之和与偶数位之和的差可以被11整除。
求对于一个b进制数,性质类似3和11的最小除数是多少。

要做出这道题,你就要知道为什么对于十进制数有这么个性质。
在下数学不太好,如果想要更加清楚详细的了解可以考虑百度。。ORZ

一个十进制数X,假设它各位数字分别为a1,a2,a3...,则该数可以表示为X=an*10^(n-1)+a(n-1)*10^(n-2)+...

对于3:
由于10与1对于3同余(即10模3的余数为1),所以十进制数X模3的余数为a1+a2+a3+...+an。
所以只要a1+a2+a3+...+an模3为0,就表明X能被3整除。

推广到b进制:也就是要找出对于b,最小的一个数i,使得b模i的结果为1.

对于11:
首先可以发现,对于10^n(n为偶数),10^n-1能被11整除(如9999,99);对于10^n(n为奇数),
10^n+1能被11整除(如1001,11)。

假设X位数为奇数(以五位数为例),
则X=a5*(10^4-1)+a4*(10^3+1)+a3*(10^2-1)+a2*(10^1+1)+a1*(10^0-1)+a5-a4+a3-a2+a1。
前面的都能被11整除,所以只要a5-a4+a3-a2+a1能被11整除,则X能被11整除。

假设X位数为偶数(以四位数为例),则在四位数基础上,万位增加一个数0(a5=0),则上方的公式仍然适用,
a5-a4+a3-a2+a1=a3+a1-a4-a2能被11整除,
说明X能被11整除。

推广到b进制:找出对于b,b+1和b*b-1的最小公约数。

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

int b;
long long bb,b0;

int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&b);
int ans1;
int ans2;
for (int i=2;i<b;i++)//枚举找出类似除数3的数
{
if (b%i==1) {ans1=i;break;}
}
bb=(long long)b*b-1;b0=b+1;
for (int i=2;i<=b0;i++)//枚举找出b+1和b*b-1的最小公约数
{
if (b0%i==0&&bb%i==0) {ans2=i;break;}
}
printf("%d %d\n",ans1,ans2);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学