1. 程式人生 > >1007 正整數分組 1010 只包含因子2 3 5的數 1014 X^2 Mod P 1024 矩陣中不重復的元素 1031 骨牌覆蓋

1007 正整數分組 1010 只包含因子2 3 5的數 1014 X^2 Mod P 1024 矩陣中不重復的元素 1031 骨牌覆蓋

str clu 重復 裏的 方法 class 如果 oid true

1007 正整數分組

將一堆正整數分為2組,要求2組的和相差最小。 例如:1 2 3 4 5,將1 2 4分為1組,3 5分為1組,兩組和相差1,是所有方案中相差最少的。 Input
第1行:一個數N,N為正整數的數量。
第2 - N+1行,N個正整數。
(N <= 100, 所有正整數的和 <= 10000)
Output
輸出這個最小差
Input示例
5
1
2
3
4
5
Output示例
1
這題不就是小李打怪獸嗎,不知道誰模仿誰,呵呵,剛還是我編的題裏的,dp,證明一下(要證明什麽自己考慮)。
 1 #include<cstdio>
 2 #include<algorithm>
 3
#include<iostream> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 8 int n,sum; 9 bool boo[10007]; 10 11 int main() 12 { 13 scanf("%d",&n); 14 boo[0]=true,sum=0; 15 int x; 16 for (int i=1;i<=n;i++) 17 { 18 scanf("%d",&x); 19 for
(int j=10000;j>=x;j--) 20 if (boo[j-x]) boo[j]=true; 21 sum+=x; 22 } 23 int i=sum/2,j=sum-i; 24 while (!boo[i]||!boo[j]) i--,j++; 25 printf("%d\n",j-i); 26 }

1010 只包含因子2 3 5的數

K的因子中只包含2 3 5。滿足條件的前10個數是:2,3,4,5,6,8,9,10,12,15。 所有這樣的K組成了一個序列S,現在給出一個數n,求S中 >= 給定數的最小的數。 例如:n = 13,S中 >= 13的最小的數是15,所以輸出15。 Input
第1行:一個數T,表示後面用作輸入測試的數的數量。(1 <= T <= 10000)
第2 - T + 1行:每行1個數N(1 <= N <= 10^18)
Output
共T行,每行1個數,輸出>= n的最小的只包含因子2 3 5的數。
Input示例
5
1
8
13
35
77
Output示例
2
8
15
36
80
枚舉處理出在範圍內的所有滿足條件的數+排序,然後二分找答案就可以了。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const LL INF=1e18+7;
10 
11 int n,cnt=0;
12 LL a[100007];
13 
14 void init()
15 {
16     for (LL i=1;i<=INF;i*=2)
17         for (LL j=1;j*i<=INF;j*=3)
18             for (LL k=1;k*i*j<=INF;k*=5)
19                 a[++cnt]=i*j*k;
20     sort(a+1,a+cnt+1);
21 }
22 int main()
23 {
24     init();
25     scanf("%d",&n);
26     LL x;
27     for (int i=1;i<=n;i++)
28     {
29         scanf("%lld",&x);
30         printf("%lld\n",*lower_bound(a+2,a+cnt+1,x));
31     }
32 } 
                  1014 X^2 Mod P
X*X mod P = A,其中P為質數。給出P和A,求<=P的所有X。 Input
兩個數P A,中間用空格隔開。(1 <= A < P <= 1000000, P為質數)
Output
輸出符合條件的X,且0 <= X <= P,如果有多個,按照升序排列,中間用空格隔開。
如果沒有符合條件的X,輸出:No Solution
Input示例
13 3
Output示例
4 9
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 int top=0,p,a;
 9 int ans[1000007]={0};
10 
11 int main()
12 {
13     scanf("%d%d",&p,&a);
14     for (int i=0;i<=p;i++)
15     {
16         long long x;
17         x=(long long)i*i;
18         if (x%p==a) ans[++top]=i;
19     }
20     if (top==0) printf("No Solution\n");
21     else
22     {
23         for (int i=1;i<top;i++)
24             printf("%d ",ans[i]);
25         printf("%d\n",ans[top]);    
26     }
27 }

1024 矩陣中不重復的元素

一個m*n的矩陣。 該矩陣的第一列是a^b,(a+1)^b,.....(a + n - 1)^b 第二列是a^(b+1),(a+1)^(b+1),.....(a + n - 1)^(b+1) ....... 第m列是a^(b + m - 1),(a+1)^(b + m - 1),.....(a + n - 1)^(b + m - 1) (a^b表示a的b次方) 下面是一個4*4的矩陣: 2^2=4, 2^3=8, 2^4=16, 2^5=32 3^2=9, 3^3=27, 3^4=81, 3^5=243 4^2=16, 4^3=64, 4^4=256, 4^5=1024 5^2=25, 5^3=125, 5^4=625, 5^5=3125 問這個矩陣裏有多少不重復的數(比如4^3 = 8^2,這樣的話就有重復了) 2^2=4, 2^3=8, 2^4=16, 2^5=32 3^2=9, 3^3=27, 3^4=81, 3^5=243 4^2=16, 4^3=64, 4^4=256, 4^5=1024 m = 4, n = 3, a = 2, b = 2。其中2^4與4^2是重復的元素。 Input
輸入數據包括4個數:m,n,a,b。中間用空格分隔。m,n為矩陣的長和寬(2 <= m,n <= 100)。a,b為矩陣的第1個元素,a^b(2 <= a , b <= 100)。
Output
輸出不重復元素的數量。
Input示例
4 3 2 2
Output示例
11
一個hash的事情。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<map>
 7 using namespace std;
 8 
 9 typedef long long LL;
10 const LL mod=132141367;
11 
12 int m,n,a,b,ans;
13 map<int,bool>p;
14 
15 int main()
16 {
17     scanf("%d%d%d%d",&m,&n,&a,&b);
18     ans=m*n;
19     for (int i=1;i<=n;i++)
20     {
21         LL x=1,jed=(a+i-1);
22         for (int j=1;j<b;j++)
23             x=x*jed%mod;    
24         for (int j=1;j<=m;j++)
25         {
26             x=x*jed%mod;
27             if (p[x]) ans--;
28             else p[x]=true;
29         }
30     }
31     printf("%d\n",ans);
32 } 

1031 骨牌覆蓋

在2*N的一個長方形方格中,用一個1*2的骨牌排滿方格。 問有多少種不同的排列方法。 例如:2 * 3的方格,共有3種不同的排法。(由於方案的數量巨大,只輸出 Mod 10^9 + 7 的結果) 技術分享 Input
輸入N(N <= 1000)
Output
輸出數量 Mod 10^9 + 7
Input示例
3
Output示例
3
比鋪磚塊要水吧,轉移的東西都少,一般的狀態壓縮。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 const int mod=1e9+7;
 9 
10 int n,m,cnt=0;
11 int f[1007][7]={0};
12 struct Node
13 {
14     int x,y;
15 }next[7];
16 
17 void dfs(int num,int sta,int old)
18 {
19     if (num>m) return;
20     if (num==m)
21     {
22         next[++cnt].x=old;
23         next[cnt].y=sta;
24         return;
25     }
26     dfs(num+1,(sta<<1)+1,old<<1);
27     dfs(num+2,sta<<2,old<<2);
28     dfs(num+1,sta<<1,(old<<1)+1);
29 }
30 int main()
31 {
32     scanf("%d",&n);
33     m=2;
34     dfs(0,0,0);
35     f[0][0]=1;
36     for (int i=1;i<=n;i++)
37         for (int j=1;j<=cnt;j++)
38         {
39             int x=next[j].x,y=next[j].y;
40             f[i][y]=(f[i][y]+f[i-1][x])%mod;
41         }
42     printf("%d\n",f[n][0]);    
43 }

1007 正整數分組 1010 只包含因子2 3 5的數 1014 X^2 Mod P 1024 矩陣中不重復的元素 1031 骨牌覆蓋