URAL 1658. Sum of Digits(簡單dp)
給你n,m。然後輸出100位以內一個數字,每一位的和為n,每一位的平方和為m。
以n,m為dp[i][j]。pre[i][j]記錄位置上的數字。
1658. Sum of Digits
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Petka thought of a positive integer n and reported to Chapaev the sum of its digits and the sum of its squared digits. Chapaev scratched his head and said: “Well, Petka, I won't find just your
number, but I can find the smallest fitting number.” Can you do the same?
Input
The first line contains the number of test cases t (no more than 10000). In each of the following tlines there are numbers s1 and s2 (1 ≤ s1, s2 ≤ 10000)
separated by a space. They are the sum of digits and the sum of squared digits of the number n.
Output
For each test case, output in a separate line the smallest fitting number n, or “No solution” if there is no such number or if it contains more than 100 digits.
Sample
input | output |
---|---|
4 9 81 12 9 6 10 7 9 |
9 No solution 1122 111112 |
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x3f3f3ff
#define PI 3.1415926535898
#define MOD 1000000009
const int maxn = 10100;
using namespace std;
int dp[910][8110];
int pre[910][8110];
void Pre()
{
for(int i = 0; i <= 900; i++)
for(int j = 0; j <= 8100; j++)
dp[i][j] = 101;
dp[0][0] = 0;
for(int i = 1; i <= 900; i++)
{
for(int j = 1; j <= 8100; j++)
{
for(int k = 1; k<=9&&k<=i&&k*k<=j; k++)
{
if(i-k > j-k*k)
break;
if(dp[i][j] > dp[i-k][j-k*k]+1)
{
dp[i][j] = dp[i-k][j-k*k]+1;
pre[i][j] = k;
}
}
}
}
}
int main()
{
Pre();
int n, m;
int T;
cin >>T;
while(T--)
{
scanf("%d %d",&n, &m);
if(n>m || n > 900 || m > 8100 || dp[n][m] > 100 )
{
cout<<"No solution"<<endl;
continue;
}
int t;
while(n&&m)
{
t = pre[n][m];
printf("%d",t);
n -= t;
m -= t*t;
}
cout<<endl;
}
return 0;
}
相關文章
- URAL 1577. E-mail(簡單二維dp)AI
- URAL 1152 False Mirrors(簡單的狀態壓縮dp)False
- LeetCode #1:Two Sum(簡單題)LeetCode
- POJ 3373 Changing Digits(記錄路徑的dp)Git
- URAL 1018 Binary Apple Tree(樹形dp入門題)APP
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- 簡單探討sum()函式返回null的問題函式Null
- HDU 1227 Fast Food(簡單二維dp)AST
- hdu 1069 Monkey and Banana(簡單dp)NaN
- 別再說SUM函式很簡單,進來看看吧!函式
- HackerRank-Coprime Power Sum-類min25篩的DP
- HDU 5119 Happy Matt Friends(簡單二維dp)APP
- HDU 1466 計算直線的交點數(簡單dp)
- 資深架構師Sum的故事:正則!入門就是這樣簡單架構
- 決策單調性DP
- Ural2110 : Remove or MaximizeREM
- Android適配:DP簡述Android
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- 演算法學習之路|POJ-2479最大子串和(簡單dp)演算法
- Add Digits 各位相加Git
- [求職 go][成都] dp 的個人簡歷求職Go
- dp 套 dp(dp of dp)小記
- hdu 3401 單調佇列+DP佇列
- 單調佇列最佳化 DP佇列
- DP套DP
- POJ 3017 單調佇列dp佇列
- hdu4374單調佇列+dp佇列
- 決策單調性最佳化DP
- Codeforces 915C Permute DigitsGit
- [LeetCode] 258. Add DigitsLeetCodeGit
- ACL Beginner Contest E.Replace DigitsGit
- LeetCode- Count Numbers with Unique DigitsLeetCodeGit
- Leetcode 258. Add DigitsLeetCodeGit
- GCD SUMGC
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- [DP] 數位DP
- YbtOJ 遞推演算法課堂過關 例5 平鋪方案【遞推(簡單DP)】演算法
- BZOJ2240 : ural1676 Mortal CombatBAT