给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019

题意:https://nanti.jisuanke.com/t/41422

对每一位进行找循环节规律就行了。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define rint register int
 24 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 25 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define pr printf
 28 #define sc scanf
 29 #define ls rt<<1
 30 #define rs rt<<1|1
 31 typedef long long ll;
 32 void swapp(int &a,int &b);
 33 double fabss(double a);
 34 int maxx(int a,int b);
 35 int minn(int a,int b);
 36 int Del_bit_1(int n);
 37 int lowbit(int n);
 38 int abss(int a);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 //const ll INF=(1LL<<60);
 42 const int inf=(1<<30);
 43 const double ESP=1e-9;
 44 const int mod=(int)1e9+7;
 45 const int N=(int)1e6+10;
 46
 47 int ans[12][60];
 48 ll m[20]={0,0,1,3,6,10,15,21,28,36,45};
 49
 50 int geet(int n,int b)
 51 {
 52     int sum=0;
 53     while(n>0)
 54     {
 55         sum+=n%b;
 56         n/=b;
 57     }
 58     return sum;
 59 }
 60
 61 ll get(ll n,ll b)
 62 {
 63     ll temp=n;
 64     int l=0;
 65     while(temp>0)
 66     {
 67         temp/=b;
 68         l++;
 69     }
 70     ll sum=0;
 71     ll val[60];
 72     val[l]=1;
 73     n++;
 74     for(int i=l-1;i>=1;--i)
 75         val[i]=val[i+1]*b;
 76     for(int i=1;i<=l;++i)
 77     {
 78         ll LOOP=n/(val[i]*b);
 79         sum+=LOOP*m[b]*val[i];
 80         ll len=n%(val[i]*b);
 81         int start=0;
 82         while(len>=val[i])
 83         {
 84             sum+=start*val[i];
 85             start++;
 86             len-=val[i];
 87         }
 88         sum+=len*start;
 89     }
 90     return sum;
 91 }
 92
 93 int main()
 94 {
 95     int T,cont=0;
 96     sc("%d",&T);
 97     while(T--)
 98     {
 99         ll n;int b;
100         sc("%lld%d",&n,&b);
101         for(int i=2;i<=10;++i)
102         {
103             ans[i][1]=1;
104             for(int j=2;j<=45;++j)
105                 ans[i][j]=ans[i][j-1]*b;
106         }
107 //        for(int i=1;i<=100;++i)
108     //        pr("%lld\n",get(i,2));
109         pr("Case #%d: %lld\n",++cont,get(n,b));
110     }
111     return 0;
112 }
113
114 /**************************************************************************************/
115
116 int maxx(int a,int b)
117 {
118     return a>b?a:b;
119 }
120
121 void swapp(int &a,int &b)
122 {
123     a^=b^=a^=b;
124 }
125
126 int lowbit(int n)
127 {
128     return n&(-n);
129 }
130
131 int Del_bit_1(int n)
132 {
133     return n&(n-1);
134 }
135
136 int abss(int a)
137 {
138     return a>0?a:-a;
139 }
140
141 double fabss(double a)
142 {
143     return a>0?a:-a;
144 }
145
146 int minn(int a,int b)
147 {
148     return a<b?a:b;
149 }

原文地址:https://www.cnblogs.com/--HPY-7m/p/11536080.html

时间: 2024-07-31 17:38:28

给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019的相关文章

51nod 1116:K进制下的大数

51nod 1116:K进制下的大数 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1116 题目大意:给定一个大数,该数在$K$进制下是$K-1$的倍数,问最小的$K$($2 \leqslant K \leqslant 36$)是多少,若无解输出No Solution. 二项式定理 这题虽然可以暴力枚举,但还有更优雅的做法. 考虑一个$K$进制的大数$A$可以被表示为$\sum_{x=0}a_xK^x$,

51NOD 1116 K进制下的大数(字符串取模 + 枚举)

传送门 1116 K进制下的大数 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一个字符串S,记录了一个大数,但不知这个大数是多少进制的,只知道这个数在K进制下是K - 1的倍数.现在由你来求出这个最小的进制K. 例如:给出的数是A1A,有A则最少也是11进制,然后发现A1A在22进制下等于4872,4872 mod 21 = 0,并且22是最小的,因此输出k = 22(大数的表示中A对应10,Z对应35). Input 输入大数对应的字符串S

假设数n在b进制下为回文数,求最小的b

题目链接:here 假设n=b0+b1+b2....+bk 如果b2<=n,那么那么n在b进制下有多个数组成,可以直接暴力算 如果暴力没有正确的结果,即: 如果b2>n,那么n在b进制下只有两个数组成 要组成回文树,则xb+x=n  b=n/x-1 ,(n%x==0,x<b),要求b最小,则使x最大, 从sqrt(n+1)开始枚举x即可 #include<iostream> #include<cstdio> #include<cmath> using

light oj 1045 - Digits of Factorial(求阶乘在不同进制下的位数)

Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170. In this problem, you

n!在k进制下的后缀0

问n! 转化成k进制后的位数和尾数的0的个数.[UVA 10061 How many zeros and how many digits?] Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many di

PAT 1015 Reversible Primes[求d进制下的逆][简单]

1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now

10进制转62进制,实现穷举指定位数的所有密码组合(暴力破解)

因为我们这里要穷举的密码包括0-9,a-z,A-Z共62个字符,所以我们采用62进制来遍历. 首先,我们实现一个10进制转62进制的方法. private static char[] charSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); //private static string[] charSet = { "0", "1&qu

light oj 1045 - Digits of Factorial K进制下N!的位数

1045 - Digits of Factorial Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 i

51nod 1116 K进制下的大数 (暴力枚举)

题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; const int MAXS = 1e5 + 10; const int MAXK = 36; char num[MAXS]; int main(int argc, const char * argv[]) { while (cin >> num) { int sum = 0; int len = (int)