But her teacher said "What if I ask you to give not only the sum but the square-sums(平方和) of all the divisors of numbers within hexadecimal(十六进制) number 100?" mmm get stuck and she's asking for your help.
Attention, because mmm has misunderstood teacher's words, you have to solve a problem that is a little bit different.
Here's the problem, given n, you are to calculate the square sums of the digits of all the divisors of n, under the base m.
n and m.(n, m would be given in 10-based)
1≤n≤10 9
2≤m≤16
There are less then 10 test cases.
OutputOutput the answer base m.Sample Input
10 2 30 5Sample Output
110
112
Hint
Use A, B, C...... for 10, 11, 12...... Test case 1: divisors are 1, 2, 5, 10 which means 1, 10, 101, 1010 under base 2, the square sum of digits is 1^2+ (1^2 + 0^2) + (1^2 + 0^2 + 1^2) + .... = 6 = 110
#include <iostream> using namespace std; char x[20]="0123456789ABCDEFG";//进制转换模板 int n,m; int f(int x) { int a[33],i=0; while(x) { a[i++]=x%m; x/=m; } int ans=0; while(i--) ans+=a[i]*a[i]; return ans; } int solve () { int i,ans=0; for(i=1;i*i<=n;i++) if(n%i==0) { ans+=f(i); if(i*i!=n)ans+=f(n/i); } return ans; } int main() { while(cin>>n>>m){ int a[33],i=0,ans=solve(); while(ans){ a[i++]=ans%m; ans/=m; } while(i--) cout<<x[a[i]]; cout<<endl; } return 0; }
3 2 1 0.5 0.5 2 1 0 1 7 7 0.32 0.16Sample Output
tiger 1.7500 wolf 1.0000 tiger 6.5968
#include <iostream> using namespace std; int main() { int t,x,y; double p,q,d1,d2; cin>>t; while(t--) { cin>>x>>y>>p>>q;
//x是指的狼y指的老虎
d1=(1-q)*x+p*q*(x+y);(bob选择狼那么Alice选择虎获得100%x加上 bob也选择虎然后他俩在一起选择狼运算式就是x+y*q*p) d2=(1-q)*p*(x+y)+q*y;(都先选择狼,再加上一虎一狼) if(d1>d2) printf("tiger %.4lf\n",d1); else printf("wolf %.4lf\n",d2); } return 0; }(题目想知道Alice是应该选择虎还是 选择狼)
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
InputThe first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.
The second line contains n integers a1, a2, ..., an (28 ≤ ai ≤ 31) — the numbers you are to check.
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
4 31 31 30 31
Yes
2 30 30
No
5 29 31 30 31 30
Yes
3 31 28 30
No
3 31 31 28
Yes
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) — March — April – May — June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December — January — February (non-leap year).
#include <iostream>
#include <stdio.h>
using namespace std;
int n ,a[3],m[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
//前面补0为了让月份与数组下标对应。
int aim[33];
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>aim[i];
//aim数组用来存储输入的月份天数
int flag=0;
int f=0;//两个标记
for(int i=1; i<=12; i++)
{
//因为输入的月份不知道是从几月开始,那么就得从十二个月进行遍历。
int len=1;//因为输入的月份是连续的所以从一开始进行累加遍历 。进行连续遍历到n为止。
while(len<=n)
{
int to=i+len-1;//to是用来存储
while(to>=13) to-=12;//如果超过了13进行换年份的调整
if(to==2)
{
if(aim[len]==29&&f==0)
{
f=1;
len++;
continue;
}
if(aim[len]==28)
{
len++;
continue;
}
//这一步关键是防止连续两年是29
//加了这个特殊判定就能保证不会两年闰年的情况
}
if(aim[len]==m[to])
{
len++;
continue;
}
break;
}
if(len==n+1)
{
flag=1;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
return 0 ;
}
Output输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output
Sample Input
2 5 20 1 3 12 2 4 10 3 5 8 1 4 6 5 7 19 6 8 17 4 7 9 8 10 16 3 9 11 10 12 15 2 11 13 12 14 20 13 15 18 11 14 16 9 15 17 7 16 18 14 17 19 6 18 20 1 13 19 5 0Sample Output
1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5 2: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5 3: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5 4: 5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5 5: 5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5 6: 5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5 7: 5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5 8: 5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5 9: 5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5 10: 5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5 11: 5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5 12: 5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5 13: 5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5 14: 5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5 15: 5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5 16: 5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5 17: 5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5 18: 5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5 19: 5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5 20: 5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5 21: 5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5 22: 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 23: 5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5 24: 5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5 25: 5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5 26: 5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5 27: 5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5 28: 5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5 29: 5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5 30: 5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5 31: 5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5 32: 5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5 33: 5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5 34: 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5 35: 5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5 36: 5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5 37: 5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5 38: 5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5 39: 5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5 40: 5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5 41: 5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5 42: 5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5 43: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 44: 5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5 45: 5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5 46: 5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5 47: 5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5 48: 5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5 49: 5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5 50: 5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5 51: 5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5 52: 5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5 53: 5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5 54: 5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5 55: 5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5 56: 5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5 57: 5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5 58: 5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5 59: 5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5 60: 5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int Path[22]; int Map[22][3]; int vis[22]; int T; int m; void dfs(int num, int step) { if(num == m && step != 0 && step < 20) return; if(num == m && step == 20) { printf("%d: %d", T ++, m); for(int i = 0; i < 20; i ++) printf(" %d", Path[i]); puts(""); } for(int i = 0; i < 3; i ++) if(!vis[Map[num][i]]) { vis[Map[num][i]] = true; Path[step] = Map[num][i]; dfs(Map[num][i], step + 1); vis[Map[num][i]] = false; } } int main(void) { for(int i = 1; i <= 20; i ++) cin >> Map[i][0] >> Map[i][1] >> Map[i][2]; while(scanf("%d", &m) == 1) { memset(vis, 0, sizeof(vis)); T = 1; dfs(m, 0); } return 0; }