码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-17 19:15:41      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:pen   ati   priority   int   users   fstream   ever   and   cas   

题意: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 }

 

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

标签:pen   ati   priority   int   users   fstream   ever   and   cas   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!