题意:求给出数的最小进制。
思路:暴力WA;
discuss中的idea:
给出数ABCD,若存在n 满足 (A* n^3 +B*n^2+C*n^1+D*n^0)%(n-1) == 0
则((A* n^3)%(n-1) +(B*n^2)%(n-1)+(C*n^1)%(n-1)+D%(n-1))%(n-1) == 0
(A+B+C+D)%(n-1) == 0
NB!
是时候深入的看下数论了;
模运算法则:
模运算与基本四则运算有些相似,但是除法例外。其规则如下:
(a + b) % p = (a % p + b % p) % p (1)
(a - b) % p = (a % p - b % p) % p (2)
(a * b) % p = (a % p * b % p) % p (3)
(a^b) % p = ((a % p)^b) % p (4)
推论:
若a≡b (% p),则对于任意的c,都有(a + c) ≡ (b + c) (%p); (10)
若a≡b (% p),则对于任意的c,都有(a * c) ≡ (b * c) (%p); (11)
若a≡b (% p),c≡d (% p),则 (a + c) ≡ (b + d) (%p),(a - c) ≡ (b - d) (%p),
(a * c) ≡ (b * d) (%p),(a / c) ≡ (b / d) (%p); (12)
费马定理:
若p是素数,a是正整数且不能被p整除,则:a^(p-1) mod p = 1 mod p
推论:
若p是素数,a是正整数且不能被p整除,则:a^p mod p = a mod p
#include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define zero_(x,y) memset(x , y , sizeof(x)) #define zero(x) memset(x , 0 , sizeof(x)) #define MAX(x) memset(x , 0x3f ,sizeof(x)) #define swa(x,y) {LL s;s=x;x=y;y=s;} using namespace std ; #define N 50005 const double PI = acos(-1.0); typedef long long LL ; int cal(char x){ if(x >= '0' && x <= '9') return x - '0'; else if(x >= 'A' && x <= 'Z') return x - 'A' +10; else if(x >= 'a' && x <= 'z') return x - 'a' +36; return 0; } string s; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(cin>>s){ int n = s.size(); int maxn = 0,sum = 0; for(int i = 0;i < n;i++){ sum +=cal(s[i]); maxn = max(maxn, cal(s[i])); } int flag = 1; for(int i = maxn+1; i <= 62; i++) if(sum%(i-1) == 0){ printf("%d ",i); flag = 0; break; } if(flag) printf("such number is impossible! "); } return 0; }