sgu 118 Digital Root
阿新 • • 發佈:2018-10-09
cas inf stream ons col str class += assert
題意:算式中結果的數字根。
先取兩數的數字根再乘起來,和先乘起來再取數字根結果一樣。因此化簡式子。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> usingnamespace std; const double EPS=1e-8; const int SZ=10030,INF=0x7FFFFFFF; typedef long long lon; lon work(lon x) { lon res=0; for(;x;) { res+=x%10; x/=10; } return res<10?res:work(res); } int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin);lon casenum; cin>>casenum; for(lon time=1;time<=casenum;++time) { lon n; cin>>n; vector<lon> vct(n); for(lon i=0;i<n;++i) { cin>>vct[i]; } lon cur=1; for(lon i=vct.size()-1;i>=0;--i) { cur*=vct[i]; cur=work(cur); if(i==0)break; cur+=1; } cout<<cur<<endl; } return 0; }
sgu 118 Digital Root